Imports DAL
Imports BAL
Imports System
Imports MyClasses
Imports System.Configuration
'Imports Reports

Public Class frmNewPI

#Region "Variable Declaratoins"
    Public _strVoucherType As String
    Dim flagVendor As Byte
    Dim _intPINO As Integer
    'Dim DontProcess As Byte = 0

    Dim intCurrentRow As Integer = 1
    'Dim CurrentCol As Integer = 1
    'Dim Status As Char

    Dim TotAmount As Double
    Dim btLoading As Byte   'While Loading Data SEt it to 1 Otherwise 0
    Dim btDelete As Byte     '0:- First Record is going to Delete  1: Second or Next record is going to delete 
    Dim intRowCount As Integer = 20  'Default no of Rows in grid
    Dim strSale As String
    Dim strSalelExcl As String
#End Region

#Region " SomeThing Extra"
    'DESIGNED & DEVELOPED BY RAHUL STARTDATE 05/01/2008 ENDDATE 30/01/2008

    'Voucher Types used (_strVoucherType)
    '1. PI -> Purchase Invoice
    '2. DC -> Dellivery Challan
    '3. DP -> DC to PI

    'Status Columns  containts
    '1.  "A"  -Active items(not yet Saved or Freezed)
    '2.  "F"  -Freeze Items(Temporary saved not saved under perticular Bill
    '3.  "U"  -Record is updated on Screen  & till not saved
    '4.  "P"  -Used to Printbill only, not to update in database 
    '5.  "S"  -Record is Saved in databser but not cleared from list(/Screen)
    '6.  "D"  -Deleted  
#End Region

#Region "Form Events"

    Private Sub frmNewPI_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        BCommon.SetTextReadOnly(txtSubTotDisply)
        BCommon.SetTextReadOnly(txtVoucherNo)
        BCommon.SetTextReadOnly(txtSchmDisc)
        BCommon.SetTextReadOnly(txtTax)
        BCommon.SetTextReadOnly(txtGrandTotal)
        BCommon.SetTextReadOnly(TextBox1)
    End Sub
    Private Sub frmNewPI_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.MdiParent = Nothing
    End Sub

    Private Sub frmNewPI_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If Not (ConfigurationManager.AppSettings("FormMode") = "R") Then
                If cmbVendorID.Enabled = True Then
                    If isUpdate() Then
                        Select Case BCommon.ShowMsgError(10081, "YNC")
                            Case Windows.Forms.DialogResult.Yes
                                e.Cancel = True
                                If ValidateData() <> 0 Then
                                    Call SaveData("A")
                                End If
                                Me.Cursor = Cursors.Default
                                Exit Sub
                            Case Windows.Forms.DialogResult.Cancel
                                e.Cancel = True
                                Me.Cursor = Cursors.Default
                                Exit Sub
                            Case Windows.Forms.DialogResult.No
                        End Select
                    End If
                    btLoading = 1
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmNewPI_FormClosing")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub frmNewPI_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        Try
            If e.KeyCode = Keys.F1 Then
                e.SuppressKeyPress = True
                If _strVoucherType = "PI" Then
                    ''Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Purchase Invoice")
                ElseIf _strVoucherType = "DP" Then
                    ''Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Delivery Challan to Purchase Invoice")
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmNewPI_KeyUp")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub frmNewPI_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            btLoading = 1
            Call BCommon.SetFormStandards(Me)
            Call BCommon.SetFlexGrid(fgItems)

            Call Grid_Settings()
            Call DisplayData()
            lblRs.Text = ConfigurationManager.AppSettings("Currency")
            lblRs1.Text = ConfigurationManager.AppSettings("Currency")
            lblRs2.Text = ConfigurationManager.AppSettings("Currency")
            cmDVendor.Focus()
            flagVendor = 1
            btLoading = 0

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmNewPI_Load")
            Me.Cursor = Cursors.Default
        Finally
            btLoading = 0
        End Try
    End Sub

    Private Sub ChkAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            Call DisplayItems()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ChkAll_CheckedChanged")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

#End Region

#Region "Button Events"

    Private Sub btnCancel_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles btnCancel.KeyDown
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then
                btnSearch.Focus()
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnCancel_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnSearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles btnSearch.KeyDown
        Try

            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then
                btnClose.Focus()
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSearch_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnSave_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles btnSave.KeyDown
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then
                btnCancel.Focus()
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSave_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Try
            Me.Close()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnClose_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            Call DisplayData()
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnCancel_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            Dim form As New frmMasterBrowse
            'Dim intcount As Integer
            Select Case _strVoucherType
                Case "PI", "DP"
                    form.strSearch = "Purchase Invoice"

                    form.StartPosition = FormStartPosition.Manual
                    form.Height = MDIPharmacy.MdiScaleHeight
                    form.Width = MDIPharmacy.MdiScaleWidth
                    Dim pt As New Point
                    pt.Y = ((MDIPharmacy.MdiScaleHeight - form.Height) / 2) + MDIPharmacy.pnlHeading.Height + MDIPharmacy.MenuStrip.Height
                    pt.X = (MDIPharmacy.MdiScaleWidth - form.Width) / 2
                    form.Location = pt
                    form.ShowInTaskbar = False

                    form.ShowDialog()
                    Dim intSearchedPI As Integer = form.strRecordNo
                    _intPINO = intSearchedPI

                    If _intPINO <> 0 Then
                        btLoading = 1
                        Call ClearGrids()
                        Call ClearData()
                        'CLEAR DATA CLEARS THE VARIABLE _intPINO 
                        _intPINO = intSearchedPI
                        Call ViewPI()
                        'cmbVendorID.  
                        btnFreeze.Enabled = False
                        btnUnFreeze.Enabled = False
                        dtpDate.Enabled = False
                        dtpBillDate.Enabled = False
                        dtpDueDate.Enabled = False
                        cmbVendorID.Enabled = False
                        cmDVendor.Enabled = False
                        cmbPayMode.Enabled = False
                        txtBillNo.Enabled = False
                        txtRemarks.Enabled = False
                        txtDisc.Enabled = False
                        txtPerc.Enabled = False
                        txtPaidAmt.Enabled = False
                        fgItems.AllowEditing = False
                        fgItems.EditOptions = C1.Win.C1FlexGrid.EditFlags.None
                        ' fgItems.Cols(15).AllowEditing = False
                        btnNewItem.Enabled = False
                        btnSave.Enabled = False
                        If _strVoucherType = "DP" Then
                            cCmbDcNo.Enabled = False
                        End If
                        btLoading = 0

                    Else : Call DisplayData()
                    End If
            End Select
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSearch_Click")
            Me.Cursor = Cursors.Default
        Finally
            btLoading = 0
        End Try
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            If ValidateData() = 0 Then
                Me.Cursor = Cursors.Default
                Exit Sub
            Else
                Call CalcCashDiscount()
                If SaveData("A") > 0 Then Call DisplayData()
            End If
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSave_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    'Private Sub btnNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewItem.Click
    '    Try
    '        Me.Cursor = Cursors.WaitCursor
    '        Dim objNewItem As New frmItemMaster
    '        objNewItem.flgItem = 1
    '        objNewItem.ShowDialog()
    '        If objNewItem.intItemNo > 0 AndAlso ChkAll.Checked = True Then DisplayItems()
    '        Me.Cursor = Cursors.Default
    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnNewItem_Click")
    '        Me.Cursor = Cursors.Default
    '    End Try
    'End Sub

    Private Sub btnBills_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBills.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            Call Display_PendingBills()
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnBills_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnFreeze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFreeze.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            If ValidateData() = 0 Then Me.Cursor = Cursors.Default : Exit Sub
            If SaveData("F") > 0 Then Call DisplayData()
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnFreeze_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnUnFreeze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnFreeze.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            Dim form As New frmMasterBrowse
            Select Case _strVoucherType
                Case "PI"
                    form.strSearch = "PI Freeze Items"
                    form.StartPosition = FormStartPosition.Manual
                    form.Height = MDIPharmacy.MdiScaleHeight
                    form.Width = MDIPharmacy.MdiScaleWidth
                    Dim pt As New Point
                    pt.Y = ((MDIPharmacy.MdiScaleHeight - form.Height) / 2) + MDIPharmacy.pnlHeading.Height + MDIPharmacy.MenuStrip.Height
                    pt.X = (MDIPharmacy.MdiScaleWidth - form.Width) / 2
                    form.Location = pt
                    form.ShowInTaskbar = False
                    form.ShowDialog()
                    '_intPINO = form.strRecordNo
                    Dim intSearchedPI As Integer = form.strRecordNo

                    btLoading = 1
                    Call ClearGrids()
                    Call ClearData()
                    btLoading = 0
                    _intPINO = intSearchedPI
                    If _intPINO <> 0 Then
                        Call ViewFreezeItems()
                        'ChkAll.Enabled = True
                        fgItems.Cols(2).Editor = cmDItems
                        Call DisplayItems()
                    Else : Call DisplayData()
                    End If
            End Select
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnUnFreeze_Click")
            Me.Cursor = Cursors.Default
        Finally
            btLoading = 0
        End Try
    End Sub


#End Region

#Region "User Defined Functions"

    Private Function SaveData(ByVal Status As Char) As Byte
        Dim btResult As Integer = 0
        Try
            Dim intResult As Integer
            Dim Payment(0) As CPayment
            Dim objBillItem() As CPurchaseItem
            Dim cPurInv As CPurchaseInvoice = SetData(Payment, objBillItem)
            If IsNothing(cPurInv) Then
                BCommon.ShowMsgError(4)
                btResult = 0
                Me.Cursor = Cursors.Default
                Exit Function
            End If

            Select Case _strVoucherType
                Case "PI", "DP"
                    intResult = DPurchaseInvoice.SaveInvoice("S", _strVoucherType, cPurInv, objBillItem, Status, Payment, "PI")
                    If intResult > 0 Then
                        btResult = 1
                        BCommon.ShowMsgError(3)
                        If Status = "A" Then
                            Call DisplayData()
                            'Divya@25/03/2007 : Changes done regarding Customize User Settings
                            If ConfigurationManager.AppSettings("ViewReport") = "Yes" Then
                                If ConfigurationManager.AppSettings("CnfrmRepMsg") = "Yes" Then
                                    If BCommon.ShowMsgError(10118, "YN") = Windows.Forms.DialogResult.Yes Then
                                        Call ViewInvoiceReport(intResult)
                                    End If
                                Else
                                    Call ViewInvoiceReport(intResult)
                                End If
                            End If
                            'Else
                            '    BCommon.ShowMsgError(3)
                        End If
                        'txtVoucherNo.Text = IIf(_strVoucherType = "DC", BCommon.GetSequentialID("DC"), BCommon.GetSequentialID("INVOICE"))

                        txtVoucherNo.Text = BCommon.GetSequentialID("INVOICE")
                    Else
                        BCommon.ShowMsgError(4)
                        btResult = 0
                    End If
            End Select
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SaveData")
            Me.Cursor = Cursors.Default
        Finally
            SaveData = btResult
        End Try
    End Function

    Private Sub ViewInvoiceReport(ByVal intResult As Integer)
        'Dim Report As New Reports.frmReportViewer
        'With Report
        '    .ReportName = "PurchaseInvoices"
        '    .strTitleName = "Purchase Invoice"
        '    ReDim .ParametersNames(0)
        '    ReDim .ParametersValues(0)
        '    .ParametersNames(0) = "InvoiceID"
        '    .ParametersValues(0) = intResult 'DPurchaseInvoice.GetInvoiceID()
        '    .ShowDialog()
        'End With
    End Sub
    Private Sub DP_Display()
        Try
            Me.Text = "Delivery Challan To Purchase Invoice"

            'ChkAll.Visible = False
            cmDVendor.SelectedIndex = -1
            ' cCmbDcNo.SelectedIndex = -1
            txtDcNo.Visible = False
            cCmbDcNo.Visible = True
            lblDCNo.Visible = True
            btnBills.Visible = True
            btnFreeze.Visible = False
            btnNewItem.Visible = False
            'btnSearch.Enabled = False
            lblRs.Visible = True
            lblBalAmt.Visible = True
            lblBalDisplay.Visible = True
            btnUnFreeze.Visible = False
            If ConfigurationManager.AppSettings("FormMode") = "R" Then
                cCmbDcNo.Enabled = False
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DP_Display")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub ViewPI()
        Dim dtDItems As DataTable
        Dim dtbBill As DataTable
        Dim intCount As Integer
        TotAmount = 0
        Try

            dtDItems = DPurchaseInvoice.GetPIItems(_intPINO)

            If dtDItems.Rows.Count > 0 Then
                With fgItems
                    If dtDItems.Rows.Count >= intRowCount Then
                        .Rows.Count = dtDItems.Rows.Count + (intRowCount / 2)
                    Else : .Rows.Count = intRowCount
                    End If
                    For intCount = 0 To dtDItems.Rows.Count - 1
                        .Item(intCount + 1, ItemGridColumns.ITEMID) = dtDItems.Rows(intCount).Item(0)        'ITEM ID
                        .Item(intCount + 1, "CODE") = dtDItems.Rows(intCount).Item(1)        '"CODE"
                        .Item(intCount + 1, 2) = dtDItems.Rows(intCount).Item(2)        '"NAME"
                        .Item(intCount + 1, 3) = dtDItems.Rows(intCount).Item(4)        '"PACKING"
                        .Item(intCount + 1, 4) = dtDItems.Rows(intCount).Item(3)        '"COMPANY"
                        .Item(intCount + 1, 5) = dtDItems.Rows(intCount).Item(5)        '"BATCH"
                        .Item(intCount + 1, 6) = Format(dtDItems.Rows(intCount).Item(6), "MMM/yy")          '"MANUFACTURED"
                        .Item(intCount + 1, 7) = Format(dtDItems.Rows(intCount).Item(7), "MMM/yy")        '"EXPIRY"
                        If dtDItems.Rows(intCount).Item(8) > 0 Then
                            .Item(intCount + 1, 8) = BScheme.GetScheme(dtDItems.Rows(intCount).Item(8))       '"SCHEMEID"
                            .Item(intCount + 1, 10) = BScheme.GetSchemeName(.Item(intCount + 1, 8))      'SCHEME DISPLAY
                        Else
                            .Item(intCount + 1, 8) = New CScheme
                            .Item(intCount + 1, 10) = dtDItems.Rows(intCount).Item(20)
                        End If
                        '.Item(intCount + 1, 10) = BScheme.GetSchemeName(.Item(intCount + 1, 8))      'SCHEME DISPLAY
                        .Item(intCount + 1, 9) = dtDItems.Rows(intCount).Item(10)      '"QUANTITY"
                        .Item(intCount + 1, 11) = " " & dtDItems.Rows(intCount).Item(9)       '"Per Pack"
                        .Item(intCount + 1, 12) = dtDItems.Rows(intCount).Item(11)      '"M.R.P."
                        .Item(intCount + 1, 13) = dtDItems.Rows(intCount).Item(12)      '"BILL RATE"
                        .Item(intCount + 1, 14) = "0"                                   '"TAX ID"

                        'VIEWTAX

                        .Item(intCount + 1, 26) = DPurchaseInvoice.GetTaxDetails(dtDItems.Rows(intCount).Item(24), .Item(intCount + 1, 0))  'TAX ARRRAY
                        .Item(intCount + 1, 15) = BTax.GetTotalTaxAmtString(.Item(intCount + 1, 26))  'TAX DISPLAY STRING

                        .Item(intCount + 1, 16) = dtDItems.Rows(intCount).Item(14)      '"AMOUNT"

                        If Not IsNothing(.Item(intCount + 1, 13)) And Not IsNothing(.Item(intCount + 1, 9)) Then
                            If Trim(.Item(intCount + 1, 9)) <> "" And Trim(.Item(intCount + 1, 13)) <> "" Then
                                .Item(intCount + 1, 17) = (CDbl(.Item(intCount + 1, 9)) * CDbl(.Item(intCount + 1, 13)) - CDbl(.Item(intCount + 1, 20)))
                            End If
                        End If
                        '>>End
                        .Item(intCount + 1, 18) = "A"                                   '"STATUS"
                        .Item(intCount + 1, 19) = dtDItems.Rows(intCount).Item(22)                                  '"VOUCHERNO"
                        .Item(intCount + 1, 20) = dtDItems.Rows(intCount).Item(21)      '"SCHEMEAMOUNT"....Vrushali...1/10/2007
                        .Item(intCount + 1, 21) = "0"                                   '"NET Price"   ....Vrushali...28/11/2007
                        .Item(intCount + 1, 22) = 0                                     'LOWEST RATE  Vrushali...30/12/2007
                        TotAmount = TotAmount + dtDItems.Rows(intCount).Item(14)
                    Next
                End With
                txtVoucherNo.Text = dtDItems.Rows(0).Item(22)
                dtpDate.Value = dtDItems.Rows(0).Item(15)
                dtpBillDate.Value = dtDItems.Rows(0).Item(16)
                dtpDueDate.Value = dtDItems.Rows(0).Item(17)

                txtBillNo.Text = dtDItems.Rows(0).Item(31)
                If dtDItems.Rows(0).Item(18) <> 0 Then
                    cmbVendorID.Text = BCommon.GetSequentialCode("VENDOR") + Trim(dtDItems.Rows(0).Item(18))
                    cmDVendor.SelectedValue = dtDItems.Rows(0).Item(18)
                Else
                    cmDVendor.SelectedIndex = 0
                End If
                If dtDItems.Rows(0).Item(23) <> 0 Then
                    cmbPayMode.SelectedValue = dtDItems.Rows(0).Item(23)
                Else
                    cmbPayMode.SelectedIndex = 0
                End If
                If cmDVendor.SelectedValue > 0 Then
                    dtbBill = DPurchaseInvoice.GetPendingBills(cmDVendor.SelectedValue)
                    If dtbBill.Rows.Count > 0 Then
                        lblBalDisplay.Text = "0.00"
                        For intCount = 0 To dtbBill.Rows.Count - 1
                            lblBalDisplay.Text = lblBalDisplay.Text + dtbBill.Rows(intCount).Item("Balance Amt")
                        Next
                    Else
                        lblBalDisplay.Text = "0.00"
                    End If
                Else
                    lblBalDisplay.Text = "0.00"
                End If

                txtRemarks.Text = dtDItems.Rows(0).Item(19)
                txtSubTotDisply.Text = BCommon.FormatAmount(Math.Round(TotAmount, 2))
                txtSchmDisc.Text = dtDItems.Rows(0).Item(28)
                txtDisc.Text = dtDItems.Rows(0).Item(29)
                txtTax.Text = BCommon.FormatAmount(Math.Round(dtDItems.Rows(0).Item(26), 2))
                txtPerc.Text = BCommon.FormatAmount(Math.Round((txtDisc.Text * 100) / (CDbl(txtSubTotDisply.Text) - CDbl(txtSchmDisc.Text)), 2))
                ' TotAmount = (TotAmount - CDbl(txtSchmDisc.Text) - (CDbl(txtDisc.Text) + CDbl(txtTax.Text)))
                txtGrandTotal.Text = BCommon.FormatAmount(Math.Round(dtDItems.Rows(0).Item("GrossAmount"), 2))
                txtPaidAmt.Text = BCommon.FormatAmount(Math.Round(dtDItems.Rows(0).Item(25), 2))
                TextBox1.Text = BCommon.FormatAmount(Math.Round(txtGrandTotal.Text - txtPaidAmt.Text, 2))

                If _strVoucherType = "DP" Then
                    Dim dtbDCNo As DataTable
                    If cmDVendor.SelectedIndex >= 0 Then
                        dtbDCNo = DPurchaseInvoice.GetAllDCNo(cmDVendor.SelectedValue)
                        If dtbDCNo.Rows.Count > 0 Then
                            cCmbDcNo.DataSource = dtbDCNo
                            cCmbDcNo.DisplayMember = "DC_No"
                            cCmbDcNo.ValueMember = "DCNo"
                            cCmbDcNo.Splits(0).DisplayColumns(0).Width() = 0
                            cCmbDcNo.SelectedValue = dtDItems.Rows(0).Item(27)
                        End If
                    End If
                End If
            Else
                btLoading = 1
                Call ClearGrids()
                btLoading = 0
                BCommon.ShowMsgError(10082)
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ViewPI")
            Me.Cursor = Cursors.Default
        Finally
            btLoading = 0
        End Try
    End Sub

    Private Sub ViewFreezeItems()
        Dim dtDItems As DataTable
        Dim dtbInvoice As DataTable
        Dim intCount As Integer
        TotAmount = 0
        Try
            btLoading = 1

            dtbInvoice = DPurchaseInvoice.GetInvoice(_intPINO)
            If dtbInvoice.Rows.Count > 0 Then
                dtDItems = DPurchaseInvoice.GetInvoiceItems(_intPINO)
                With fgItems
                    If dtDItems.Rows.Count >= intRowCount Then
                        .Rows.Count = dtDItems.Rows.Count + (intRowCount / 2)
                    Else : .Rows.Count = intRowCount
                    End If

                    For intCount = 0 To dtDItems.Rows.Count - 1
                        .Item(intCount + 1, ItemGridColumns.ITEMID) = dtDItems.Rows(intCount).Item(0)        'ITEM ID
                        .Item(intCount + 1, "CODE") = dtDItems.Rows(intCount).Item(1)        '"CODE"
                        .Item(intCount + 1, 2) = dtDItems.Rows(intCount).Item(2)        '"NAME"
                        .Item(intCount + 1, 3) = dtDItems.Rows(intCount).Item(4)        '"PACKING"
                        .Item(intCount + 1, 4) = dtDItems.Rows(intCount).Item(3)        '"COMPANY"
                        .Item(intCount + 1, 5) = dtDItems.Rows(intCount).Item(5)        '"BATCH"
                        .Item(intCount + 1, 6) = Format(dtDItems.Rows(intCount).Item(6), "MMM/yy")       '"MANUFACTURED"
                        .Item(intCount + 1, 7) = Format(dtDItems.Rows(intCount).Item(7), "MMM/yy")  '"EXPIRY"
                        If dtDItems.Rows(intCount).Item(8) > 0 Then   '8-SCHEMEID
                            .Item(intCount + 1, 8) = BScheme.GetScheme(dtDItems.Rows(intCount).Item(8))       '"SCHEMEID"
                            .Item(intCount + 1, 10) = BScheme.GetSchemeName(.Item(intCount + 1, 8))      'SCHEME DISPLAY
                        Else
                            .Item(intCount + 1, 8) = New CScheme
                            .Item(intCount + 1, 10) = dtDItems.Rows(intCount).Item(14)
                        End If

                        .Item(intCount + 1, 9) = dtDItems.Rows(intCount).Item(10)      '"QUANTITY"
                        .Item(intCount + 1, 11) = dtDItems.Rows(intCount).Item(9)       '"Per Pack"
                        .Item(intCount + 1, 12) = dtDItems.Rows(intCount).Item(11)      '"M.R.P."
                        .Item(intCount + 1, 13) = dtDItems.Rows(intCount).Item(12)      '"BILL RATE"
                        .Item(intCount + 1, 14) = "0"                                   '"TAX ID"

                        .Item(intCount + 1, 26) = DPurchaseInvoice.GetTaxDetails(_intPINO, .Item(intCount + 1, 0))  'TAX ARRRAY
                        .Item(intCount + 1, 15) = BTax.GetTotalTaxAmtString(.Item(intCount + 1, 26))  'TAX DISPLAY STRING
                        .Item(intCount + 1, 16) = dtDItems.Rows(intCount).Item(13)      '"AMOUNT"

                        If Not IsNothing(.Item(intCount + 1, 13)) And Not IsNothing(.Item(intCount + 1, 9)) Then
                            If Trim(.Item(intCount + 1, 9)) <> "" And Trim(.Item(intCount + 1, 13)) <> "" Then
                                .Item(intCount + 1, 17) = (CDbl(.Item(intCount + 1, 9)) * CDbl(.Item(intCount + 1, 13)) - CDbl(.Item(intCount + 1, 20)))
                            End If
                        End If
                        .Item(intCount + 1, 18) = "A"                                   '"STATUS"
                        .Item(intCount + 1, 19) = _intPINO                              '"VOUCHERNO"
                        .Item(intCount + 1, 20) = dtDItems.Rows(intCount).Item(16)      '"SCHEMEAMOUNT"....Vrushali...1/10/2007
                        .Item(intCount + 1, 21) = "0"                                   '"NET Price"   ....Vrushali...28/11/2007
                        .Item(intCount + 1, 22) = 0                                     'LOWEST RATE  Vrushali...30/12/2007
                        .Item(intCount + 1, 24) = dtDItems.Rows(intCount).Item(14)      'SCHEME QUANTITY   
                        .Item(intCount + 1, 25) = dtDItems.Rows(intCount).Item(16)      'ITEM DISCOUNT 
                        .Item(intCount + 1, 27) = dtDItems.Rows(intCount).Item(15)      'scheme discount
                        TotAmount = TotAmount + dtDItems.Rows(intCount).Item(13)        'AMOUNT
                    Next
                End With

                txtVoucherNo.Text = dtbInvoice.Rows(0).Item(1)
                dtpDate.Value = dtbInvoice.Rows(0).Item(2)
                dtpBillDate.Value = dtbInvoice.Rows(0).Item(4)
                dtpDueDate.Value = dtbInvoice.Rows(0).Item(16)

                txtBillNo.Text = dtbInvoice.Rows(0).Item(3)
                If dtbInvoice.Rows(0).Item(6) <> 0 Then
                    cmbVendorID.Text = BCommon.GetSequentialCode("VENDOR") + Trim(dtbInvoice.Rows(0).Item(6))
                    cmDVendor.SelectedValue = dtbInvoice.Rows(0).Item(6)
                Else
                    cmDVendor.SelectedIndex = 0
                End If

                Show_PendingBills(False)

                If dtDItems.Rows(0).Item(13) <> 0 Then
                    cmbPayMode.SelectedValue = dtbInvoice.Rows(0).Item(13)
                Else
                    cmbPayMode.SelectedIndex = 0
                End If

                txtRemarks.Text = dtbInvoice.Rows(0).Item(15)
                txtSubTotDisply.Text = BCommon.FormatAmount(Math.Round(CDbl(dtbInvoice.Rows(0).Item(7)), 2))
                txtSchmDisc.Text = BCommon.FormatAmount(CDbl(dtbInvoice.Rows(0).Item(8)))
                txtTax.Text = BCommon.FormatAmount(Math.Round(CDbl(dtbInvoice.Rows(0).Item(9)), 2))
                txtDisc.Text = BCommon.FormatAmount(CDbl(dtbInvoice.Rows(0).Item(10)))
                txtGrandTotal.Text = BCommon.FormatAmount(CDbl(dtbInvoice.Rows(0).Item(11)))
                txtPerc.Text = Math.Round((txtDisc.Text * 100) / (CDbl(txtSubTotDisply.Text) - CDbl(txtSchmDisc.Text)), 2)
                txtPaidAmt.Text = Math.Round(CDbl(dtbInvoice.Rows(0).Item(12)), 2)
                TextBox1.Text = BCommon.FormatAmount(Math.Round(txtGrandTotal.Text - txtPaidAmt.Text, 2))

            Else
                Call ClearGrids()
                BCommon.ShowMsgError(10082)
            End If
            btLoading = 0
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ViewFreezeItems")
            Me.Cursor = Cursors.Default
        Finally
            btLoading = 0
        End Try
    End Sub

    Private Function ValidateData() As Byte
        Try
            If Trim(txtVoucherNo.Text) = String.Empty Then
                BCommon.ShowMsgError(10118)
                Me.Cursor = Cursors.Default
                Return 0
            End If
            If cmDVendor.SelectedValue <= 0 Then
                BCommon.ShowMsgError(10278)
                cmDVendor.Focus()
                Me.Cursor = Cursors.Default
                Return 0
            End If

            With fgItems
                If IsNothing(fgItems.Item(1, 0)) OrElse Trim(fgItems.Item(1, 0)) = String.Empty OrElse fgItems.Item(1, 0) = 0 Then
                    BCommon.ShowMsgError(10287)
                    Me.Cursor = Cursors.Default
                    Return 0
                End If

                For intCnt As Integer = 1 To .Rows.Count - 1
                    If IsNothing(.Item(intCnt, 0)) OrElse Trim(.Item(intCnt, 0)) = String.Empty OrElse .Item(intCnt, 0) = 0 Then Me.Cursor = Cursors.Default : Exit For
                    If isValidRow(intCnt) > 0 Then 'Row is field completely
                        Me.Cursor = Cursors.Default
                        Return 0
                    End If
                Next
            End With
            Return 1
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ValidateData")
            Me.Cursor = Cursors.Default
        End Try
    End Function


    Private Function SetData(ByRef Payment() As CPayment, ByRef objBillItem() As CPurchaseItem) As CPurchaseInvoice
        Dim intCount As Integer ', intResult As Integer
        TotAmount = 0
        Dim cPurInv As CPurchaseInvoice
        'Dim ItemTaxes() As TaxPI
        Try
            cPurInv = New CPurchaseInvoice
            With cPurInv
                .InvoiceID = IIf(_intPINO <> 0, _intPINO, 0)
                .InvoiceSeqID = Trim(txtVoucherNo.Text)
                .VoucherDate = dtpDate.Value
                .VendorID = cmDVendor.SelectedValue()
                .DcID = IIf(_strVoucherType = "PI", Trim(txtDcNo.Text), cCmbDcNo.SelectedValue())
                .BillNo = Trim(txtBillNo.Text)
                .PayMode = IIf(Not IsNothing(cmbPayMode.SelectedValue), cmbPayMode.SelectedValue, String.Empty)
                .BillDate = dtpBillDate.Value
                .DueDate = dtpDueDate.Value
                .TotalTax = Format(CDbl(Trim(txtTax.Text)), "0.00")
                .TotalDisc = Format(CDbl(Trim(txtDisc.Text)), "0.00")
                .SchmDisc = Format(CDbl(Trim(txtSchmDisc.Text)), "0.00")
                .PaidAmt = Format(CDbl(Trim(txtPaidAmt.Text)), "0.00")
                .InvoiceAmt = Format(CDbl(txtSubTotDisply.Text), "0.00")
                .GrossAmt = Format(CDbl((.InvoiceAmt - txtSchmDisc.Text - txtDisc.Text) + txtTax.Text), "0.00")    'txtSubTotDisply.Text
                .Remarks = txtRemarks.Text
            End With

            Dim intItems As Integer
            For intCount = 1 To fgItems.Rows.Count - 2
                If Not IsNothing(fgItems.Item(intCount, 0)) Then
                    If Trim(fgItems.Item(intCount, 0)) <> "" OrElse Trim(fgItems.Item(intCount, 0)) <> "0" Then
                        intItems = intItems + 1
                    End If
                End If
            Next

            ReDim objBillItem(intItems - 1)

            ' Dim intManufacurer As Integer
            Dim dtbVItems As New DataTable
            For intCount = 0 To intItems - 1
                objBillItem(intCount) = New CPurchaseItem
                With fgItems
                    objBillItem(intCount).ItemID = .Item(intCount + 1, ItemGridColumns.ITEMID)
                    objBillItem(intCount).CLRcode = .Item(intCount + 1, ItemGridColumns.CLRID)
                    'objBillItem(intCount).ManufactureDate = .Item(intCount + 1, 6)
                    'objBillItem(intCount).ExpiryDate = BCommon.GetDatewithLastDay(.Item(intCount + 1, 7))

                    'ITEMS WHICH R NOT TILL SUPPLIED BY BY THAT VENDOR BUT NOW STARTED SUPPLING NEW ITEM
                    'dtbVItems = DItem.GetvendorItems(cmDVendor.SelectedValue, .Item(intCount + 1, 0))
                    'If dtbVItems.Rows.Count <= 0 Then
                    '    intManufacurer = DManufacturer.GetManufacturer(.Item(intCount + 1, 0))
                    '    intResult = DItem.SaveItemsMV(intManufacurer, cmDVendor.SelectedValue, .Item(intCount + 1, 0))
                    '    If intResult <= 0 Then Return Nothing 'Exit Function

                    'End If
                    'Dim Scheme As CScheme = .Item(intCount + 1, 8)
                    'If IsNothing(Scheme) OrElse Scheme.SchemeID = 0 Then
                    '    objBillItem(intCount).SchemeID = 0
                    '    objBillItem(intCount).SchemeDiscount = 0
                    '    objBillItem(intCount).SchemeItemQty = IIf(Trim(.Item(intCount + 1, 24)) = "", 0, Val(.Item(intCount + 1, 24)))
                    '    objBillItem(intCount).StockQuantity = Val(.Item(intCount + 1, 9) + objBillItem(intCount).SchemeItemQty)
                    'Else
                    '    objBillItem(intCount).SchemeID = Scheme.SchemeID
                    '    If Scheme.schemeItemID > 0 Then 'FREE ITEM SCHEME
                    '        objBillItem(intCount).SchemeDiscount = 0
                    '        objBillItem(intCount).SchemeItemQty = IIf(IsNothing(.Item(intCount + 1, 24)) OrElse Trim(.Item(intCount + 1, 24)) = String.Empty, 0, .Item(intCount + 1, 24))

                    '        If Scheme.schemeItemID = objBillItem(intCount).ItemID AndAlso Scheme.ApllyTo = "P" Then
                    '            objBillItem(intCount).StockQuantity = Val(.Item(intCount + 1, 9)) + IIf(Trim(.Item(intCount + 1, 24)) = "", 0, Val(.Item(intCount + 1, 24)))
                    '        Else
                    '            objBillItem(intCount).StockQuantity = Val(.Item(intCount + 1, 9))
                    '        End If

                    '    Else 'PRICE RELATED SCHME
                    '        objBillItem(intCount).SchemeItemQty = 0
                    '        objBillItem(intCount).SchemeDiscount = IIf(IsNothing(.Item(intCount + 1, 27)) OrElse Trim(.Item(intCount + 1, 27)) = String.Empty, 0, .Item(intCount + 1, 27))
                    '        objBillItem(intCount).StockQuantity = Val(.Item(intCount + 1, 9))
                    '    End If
                    'End If

                    'objBillItem(intCount).ItemDiscount = IIf(IsNothing(.Item(intCount + 1, 25)) OrElse Trim(.Item(intCount + 1, 25)) = String.Empty, 0, CDbl(.Item(intCount + 1, 25)))
                    objBillItem(intCount).Quantity = .Item(intCount + 1, ItemGridColumns.QTY)

                    'objBillItem(intCount).Pack = .Item(intCount + 1, 11)
                    'objBillItem(intCount).StockQuantity = IIf(objBillItem(intCount).SchemeID > 0, Val(.Item(intCount + 1, 9)), Val(.Item(intCount + 1, 9)) + .Item(intCount + 1, 24))
                    'objBillItem(intCount).MRP = .Item(intCount + 1, 12)
                    objBillItem(intCount).BillRate = .Item(intCount + 1, 13)
                    ' objBillItem(intCount).SalePrice1 = Format(CDbl(.Item(intCount + 1, 17)), "0.00")
                    ' objBillItem(intCount).PurchasePrice = .Item(intCount + 1, 13)
                    ' objBillItem(intCount).Net_Rate = Math.Round(.Item(intCount + 1, 21), 2) 'Vrushali....29/11/2007

                    'Code Start For Tax                Vrushali.....20/09/2007
                    'If _strVoucherType <> "DC" Then
                    'ItemTaxes = .Item(intCount + 1, 26)
                    'If Not IsNothing(ItemTaxes) AndAlso ItemTaxes.Length > 0 Then
                    '    ReDim objBillItem(intCount).Tax(ItemTaxes.Length - 1)

                    '    For intCnt As Integer = 0 To ItemTaxes.Length - 1
                    '        objBillItem(intCount).Tax(intCnt) = New CTax
                    '        objBillItem(intCount).Tax(intCnt).ItemID = .Item(intCount + 1, 0)
                    '        objBillItem(intCount).Tax(intCnt).BillNo = txtBillNo.Text
                    '        objBillItem(intCount).Tax(intCnt).CLRcode = .Item(intCount + 1, 5)
                    '        objBillItem(intCount).Tax(intCnt).TaxID = ItemTaxes(intCnt).TaxID
                    '        objBillItem(intCount).Tax(intCnt).TaxAmount = ItemTaxes(intCnt).TaxAmount
                    '        objBillItem(intCount).Tax(intCnt).Percent = ItemTaxes(intCnt).TaxValue
                    '        objBillItem(intCount).Tax(intCnt).ApplyTo = ItemTaxes(intCnt).ApplyTo
                    '    Next
                    'Else
                    '    objBillItem(intCount).Tax = Nothing
                    'End If
                    'End If
                End With
            Next

            'Payment(0) = New CPayment
            'With Payment(0)
            '    .PayType = "Pay"
            '    .BillType = "Purchase"
            '    .PayerID = cPurInv.VendorID
            '    .PayDate = Now.Date
            '    .PayMode = cPurInv.PayMode
            '    .PayAmount = cPurInv.PaidAmt
            '    .Concession = cPurInv.TotalDisc
            '    Select Case .PayMode
            '        Case 2
            '            .Bankname = FGCheqDetails.Rows(1).Item(0)
            '            .ChequeNo = FGCheqDetails.Rows(1).Item(1)
            '            .ChqDate = FGCheqDetails.Rows(1).Item(2)
            '        Case 3
            '            .Bankname = FGCheqDetails.Rows(1).Item(0)
            '            .Cardno = FGCardDetails.Rows(1).Item(1)
            '            .Cardtype = ccmbCardtype.SelectedValue
            '            .Holdername = FGCardDetails.Rows(1).Item(3)
            '    End Select
            'End With
            Return cPurInv
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetData")
            Me.Cursor = Cursors.Default
            Return Nothing
        End Try

    End Function

    Private Sub SetDatepickerMaxMinDate()
        Dim dtDatePkr As DataTable
        Try
            dtDatePkr = BCommon.GetDatePicker()
            If dtDatePkr.Rows.Count > 0 Then
                dtpBillDate.MinDate = dtDatePkr.Rows(0).Item("fin_start_dt")
                dtpBillDate.MaxDate = dtDatePkr.Rows(0).Item("fin_end_dt")
                dtpDueDate.MinDate = dtDatePkr.Rows(0).Item("fin_start_dt")
                dtpDueDate.MaxDate = dtDatePkr.Rows(0).Item("fin_end_dt")
                dtpDate.MinDate = dtDatePkr.Rows(0).Item("fin_start_dt")
                dtpDate.MaxDate = dtDatePkr.Rows(0).Item("fin_end_dt")
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetDatepickerMaxMinDate")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub DisplayData()

        Try
            btLoading = 1
            Call ClearGrids()
            Call ClearData()

            Call SetDatepickerMaxMinDate()   'Divya 22/11/2007

            fgItems.Cols(2).Editor = Nothing

            '            ChkAll.Checked = False              '**
            '           ChkAll.Enabled = False
            fgBills.Visible = False
            lblGrndTot.Visible = True
            lblGrndTot.Text = "Grand Total"
            lblTaxDisc.Visible = True
            lblSubTot.Visible = True
            lblSubTot.Text = "Total"
            lblDisc.Visible = True
            txtGrandTotal.Visible = True
            txtTax.Visible = True
            txtDisc.Visible = True
            txtSchmDisc.Visible = True
            lblSchmDisc.Visible = True
            btnFreeze.Enabled = False

            btnBills.Text = "Sho&w Bills"
            'btnFreeze.Visible = True

            Dim dtDVendor As DataTable
            dtDVendor = DVendor.GetVendors()

            With cmDVendor
                .DataSource = dtDVendor
                .Splits(0).DisplayColumns(0).Visible = False
                .Splits(0).DisplayColumns(1).Width = .Width * 26 / 100
                .Splits(0).DisplayColumns(2).Width = .Width * 74 / 100
                .Columns(1).Caption = "CODE"
                .Columns(2).Caption = "NAME"
                .ValueMember = "VendorID"
                .DisplayMember = "VendorName"

                .AutoCompletion = True
                .AutoDropDown = True
                .AutoSelect = True
                .AutoSize = False
                .AllowSort = False
            End With
            With cmbVendorID
                .DataSource = dtDVendor
                .Splits(0).DisplayColumns(0).Visible = False
                .DropDownWidth = cmDVendor.DropDownWidth
                .Splits(0).DisplayColumns(1).Width = cmDVendor.Width * 26 / 100
                .Splits(0).DisplayColumns(2).Width = cmDVendor.Width * 74 / 100
                .Columns(1).Caption = "CODE"
                .Columns(2).Caption = "NAME"
                .ValueMember = "VendorID"
                .DisplayMember = "seqVendorID"

                .AutoCompletion = True
                .AutoDropDown = True
                .AutoSelect = True
                .AutoSize = False
                .AllowSort = False
            End With
            Dim dtbPack As DataTable = DSale.GetPackList()
            With CmbPack
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .DataSource = dtbPack
                .ValueMember = "PACKID"
                .DisplayMember = "PACK"
            End With
            'Call DP_Display()
            'Select Case _strVoucherType
            '    Case "PI"  'Purchase Invoice
            '        Me.Text = "Purchase Invoice"
            '        'BCommon.SetComboValues(cmDVendor, DVendor.GetVendor(), "VendorID", "VendorName")
            '        cmDVendor.DataSource = DVendor.GetVendor()
            '        cmDVendor.ValueMember = cmDVendor.DataSource.Columns("VendorID").ToString()
            '        cmDVendor.DisplayMember = cmDVendor.DataSource.Columns("VendorName").ToString()
            '    Case "DP"  'Dellivery Challan to Purchase Invoice
            '        Dim dtDVendor As DataTable
            '        dtDVendor = DVendor.GetVendors()
            '        cmDVendor.DataSource = dtDVendor
            '        cmDVendor.ValueMember = "VendorID"
            '        cmDVendor.DisplayMember = "VendorName"
            '        Call DP_Display()
            'End Select

            'BCommon.SetComboValues(cmDVendor, DVendor.GetVendor(), "VendorID", "VendorName")
            BCommon.SetComboValues(cmbPayMode, "PAYMODE")
            fgItems.Rows.Count = intRowCount
            'txtVoucherNo.Text = IIf(_strVoucherType = "DC", BCommon.GetSequentialID("DC"), BCommon.GetSequentialID("INVOICE"))

            txtVoucherNo.Text = BCommon.GetSequentialID("INVOICE")

            txtBillNo.Focus()
            flagVendor = 1
            btLoading = 0
            'Divya 30/01/2008 : Added for ReadOnly fields

            '>>End
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisplayData")
            Me.Cursor = Cursors.Default
        Finally
            btLoading = 0
        End Try
    End Sub

    Private Sub ClearData()
        Try
            If ConfigurationManager.AppSettings("FormMode") = "R" Then
                btnUnFreeze.Enabled = False
                btnNewItem.Enabled = False
                btnSave.Enabled = False
                btnFreeze.Enabled = False
                txtDcNo.Enabled = False
                dtpDate.Enabled = False
                dtpBillDate.Enabled = False
                dtpDueDate.Enabled = False
                cmbVendorID.Enabled = False
                cmDVendor.Enabled = False
                cmbPayMode.Enabled = False
                txtBillNo.Enabled = False
            Else
                btnUnFreeze.Enabled = True
                btnNewItem.Enabled = True
                btnSave.Enabled = True
                btnFreeze.Enabled = True
                txtDcNo.Enabled = True
                dtpDate.Enabled = True
                dtpBillDate.Enabled = True
                dtpDueDate.Enabled = True
                cmbVendorID.Enabled = True
                cmDVendor.Enabled = True
                cmbPayMode.Enabled = True
                txtBillNo.Enabled = True
            End If
            flagVendor = 0
            _intPINO = 0
            'ChkAll.Checked = False
            'ChkAll.Enabled = False
            txtVoucherNo.Text = ""
            cmDVendor.SelectedIndex = -1
            cCmbDcNo.SelectedIndex = -1
            lblBalDisplay.Text = "0.00"
            cCmbDcNo.DataSource = Nothing
            txtDcNo.Text = ""
            txtBillNo.Text = ""
            cmbPayMode.SelectedIndex = -1
            'txtTax.Text = ""
            txtSubTotDisply.Text = "0.00"
            txtPaidAmt.Text = "0.00"
            TextBox1.Text = "0.00"
            txtGrandTotal.Text = "0.00"
            'lblSubTot.Text = "0.00"
            txtTax.Text = "0.00"
            txtDisc.Text = "0.00"
            txtPerc.Text = "0"
            txtSchmDisc.Text = "0.00"
            cmbVendorID.Text = ""
            cmDItems.Visible = False
            txtRemarks.Text = ""
            btnBills.Enabled = False

            If ConfigurationManager.AppSettings("FormMode") = "R" Then
                txtRemarks.Enabled = False
                txtDisc.Enabled = False
                txtPerc.Enabled = False
                txtPaidAmt.Enabled = False
            Else
                txtRemarks.Enabled = True
                txtDisc.Enabled = True
                txtPerc.Enabled = True
                txtPaidAmt.Enabled = True
            End If

            cCmbDcNo.Enabled = True

            BCommon.SetTodaysDate(dtpDate)
            BCommon.SetTodaysDate(dtpBillDate)
            BCommon.SetTodaysDate(dtpDueDate)
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ClearData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub


    Public Sub ClearGrids()
        Try
            intCurrentRow = 1
            'CurrentCol = 1
            fgItems.Col = 1
            With fgItems
                If .Rows.Count > 1 Then
                    '.Clear(C1.Win.C1FlexGrid.ClearFlags.Content, 1, 0, .Rows.Count - 1, .Cols.Count - 1)
                    '.Clear(C1.Win.C1FlexGrid.ClearFlags.Style, 1, 1, .Rows.Count - 1, 1)
                    .Rows.RemoveRange(1, .Rows.Count - 1)
                End If
            End With
            fgItems.AllowEditing = True
            fgItems.EditOptions = C1.Win.C1FlexGrid.EditFlags.All
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ClearGrids")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Public Sub CheckSetItemRow(ByVal intRow As Integer)
        Try
            With fgItems
                If IsNothing(.Item(intRow, 0)) OrElse Trim(.Item(intRow, 0)) = String.Empty Then Me.Cursor = Cursors.Default : Exit Sub
                If IsNothing(.Item(intRow, 9)) OrElse Trim(.Item(intRow, 9)) = String.Empty Then
                    '  SetItemRow("A", intRow, True)
                    ' Else : SetItemRow("A", intRow, False)
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "CheckSetItemRow")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    'Public Sub SetItemRow(ByVal ItemStatus As Char, ByVal intRow As Integer, ByVal IsNewItem As Boolean)
    '    'Sets the default values for selected row
    '    Dim dtbTable As New DataTable
    '    Dim DiscAmnt As Double = 0
    '    Dim strItem As String = String.Empty
    '    Dim strBatch As String = String.Empty
    '    Try
    '        With fgItems

    '            dtbTable = DItem.GetItemDtls(.Item(intRow, 0), cmDVendor.SelectedValue, True)

    '            If dtbTable.Rows.Count = 0 Then Me.Cursor = Cursors.Default : Exit Sub
    '            .Item(intRow, ItemGridColumns.ITEMID) = dtbTable.Rows(0).Item(0)                                         'ItemID
    '            .Item(intRow, ItemGridColumns.ITEMCODE) = dtbTable.Rows(0).Item(1)                            'Seq ItemID
    '            .Item(intRow, ItemGridColumns.ITEMNAME) = dtbTable.Rows(0).Item(2)                                         'Name

    '            .Item(intRow, ItemGridColumns.CLRDESC) = dtbTable.Rows(0).Item(3)                                         'Company
    '            '.Item(intRow, 11) = DItem.GetConversionFactor(dtbTable.Rows(0).Item(0))             'Per Pack
    '            '.Item(intRow, 18) = ItemStatus                                        '"STATUS"
    '            intCurrentRow = intRow


    '            Call CalcTotalAmount()
    '        End With

    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetItemRow")
    '        Me.Cursor = Cursors.Default
    '    Finally
    '        btLoading = 0
    '    End Try
    'End Sub

    Private Function IsVendorSelected() As Byte
        Try
            With fgItems
                Select Case .Col
                    Case 1, 2
                        If Not cmDVendor.SelectedValue > 0 Then
                            .Rows(.Row).Clear(C1.Win.C1FlexGrid.ClearFlags.Content)
                            BCommon.ShowMsgError(10278)
                            Return 0
                        End If
                End Select
            End With
            Return 1
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "IsVendorSelected")
            Me.Cursor = Cursors.Default
            Return 0
        End Try
    End Function

    Private Sub SetItemRowDefltVals(ByVal intRow As Integer)
        Try
            With fgItems
                btLoading = 1
                For intCount As Integer = 5 To .Cols.Count - 1
                    .Item(intRow, intCount) = Nothing
                Next
                btLoading = 0
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetItemRowDefltVals")
            Me.Cursor = Cursors.Default
        Finally
            btLoading = 0
        End Try
    End Sub

    Private Function isValidRow(ByVal intCurrentRow As Integer) As Integer
        Try
            With fgItems

                ' IS ITEM SELECTED 
                If IsNothing(.Item(intCurrentRow, ItemGridColumns.ITEMCODE)) OrElse Trim(.Item(intCurrentRow, ItemGridColumns.ITEMCODE)) = "" Then Return ShowColError(1)

                'IS PACK ENTERD
                If IsNothing(.Item(intCurrentRow, ItemGridColumns.PACK)) OrElse Trim(.Item(intCurrentRow, ItemGridColumns.PACK)) = "" Then .Col = ItemGridColumns.PACK : Return ShowColError(ItemGridColumns.PACK)

                'IS MANUFACTURING DATE ENTERED PROPERLY
                If IsNothing(.Item(intCurrentRow, 6)) OrElse Trim(.Item(intCurrentRow, 6)) = "" Then .Col = 6 : Return ShowColError(6)

                'IS EXPIRY ENTERED PROPERLY
                '  If IsNothing(.Item(intCurrentRow, 7)) OrElse Trim(.Item(intCurrentRow, 7)) = "" Then .Col = 7 : Return ShowColError(7)

                'IS QUANTITY ENTERD 
                If IsNothing(.Item(intCurrentRow, ItemGridColumns.QTY)) OrElse Trim(.Item(intCurrentRow, ItemGridColumns.QTY)) <= 0 Then Return ShowColError(ItemGridColumns.QTY)
                'IS COLOR SELECTED 

                If IsNothing(.Item(intCurrentRow, ItemGridColumns.CLRID)) OrElse Trim(.Item(intCurrentRow, ItemGridColumns.CLRID)) <= 0 Then Return ShowColError(ItemGridColumns.QTY)


                'If _strVoucherType = "PI" Or _strVoucherType = "DP" Then
                '    'IS MRP Entered 
                '    If IsNothing(.Item(intCurrentRow, 12)) OrElse Trim(.Item(intCurrentRow, 12)) <= 0 OrElse Trim(.Item(intCurrentRow, 12)) = "" Then Return ShowColError(12)
                '    'IS BILL RATE ENTERED 
                '    If IsNothing(.Item(intCurrentRow, 13)) OrElse Trim(.Item(intCurrentRow, 13)) <= 0 Then Return ShowColError(13) 'Return 0
                'Else
                '    Return 0
                'End If

                ''IS SALE RATE ENTERED 
                'If IsNothing(.Item(intCurrentRow, 17)) OrElse Trim(.Item(intCurrentRow, 17)) <= 0 Then Return 0
                Return 0

            End With

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "isValidRow")
            Me.Cursor = Cursors.Default
            Return 0
        End Try

    End Function

    Private Function ShowColError(ByVal intCol As Integer) As Byte
        Try

            Select Case intCol
                Case 0 'Item
                    BCommon.ShowMsgError(10111)
                    intCol = 1

                Case ItemGridColumns.PACK : MessageBox.Show("SELECT PACK") 'BCommon.ShowMsgError(10112)  '"BATCH"
                Case ItemGridColumns.CLRDESC : MessageBox.Show("SLECT COLOR")
                    'Case 7 : BCommon.ShowMsgError(10272)  '"EXPIRY"
                Case ItemGridColumns.QTY : BCommon.ShowMsgError(10232)  '"QUANTITY"
                    'Case 12 : BCommon.ShowMsgError(10115) '"M.R.P."
                Case ItemGridColumns.RATE : BCommon.ShowMsgError(10116) '"BILL RATE"
                    'Case 17 : BCommon.ShowMsgError(10117) '"SALE RATE"
                Case Else : intCol = 0

            End Select

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ShowColError")
            Me.Cursor = Cursors.Default
        Finally
            ShowColError = intCol
        End Try
    End Function

    Private Sub Display_PendingBills()
        Try
            Dim dtbBill As New DataTable
            With fgBills
                If .Visible = True Then
                    .Visible = False
                    btnBills.Text = "Sho&w Bills"
                    fgItems.Focus()
                Else
                    btnBills.Text = "&Hide Bills"
                    dtbBill = DPurchaseInvoice.GetPendingBills(cmDVendor.SelectedValue)

                    .DataSource = dtbBill
                    .Cols(4).Format = "0.00"
                    .Cols(5).Format = "0.00"
                    .Cols(6).Format = "0.00"
                    .Cols(7).Format = "dd/MM/yy"
                    .Cols(0).Visible = False

                    .Visible = True
                    .Focus()
                    .Row = 1
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "Display_PendingBills")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub CalcTotalAmount()
        Try
            TotAmount = 0.0
            Call CalcDiscAmount()
            TotAmount = TotAmount + CalcDiscAmount()
            txtSubTotDisply.Text = BCommon.FormatAmount(Math.Round(TotAmount, 2))
            txtGrandTotal.Text = BCommon.FormatAmount(Math.Round((((CDbl(txtSubTotDisply.Text) - CDbl(txtSchmDisc.Text)) - CDbl(txtDisc.Text)) + CDbl(txtTax.Text)), 2))

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "CalcTotalAmount")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub Show_PendingBills(ByVal ShowGrid As Boolean)
        Try
            lblBalDisplay.Text = "0.00"
            If cmDVendor.SelectedValue > 0 Then
                Dim dtbBill As DataTable = DPurchaseInvoice.GetPendingBills(cmDVendor.SelectedValue)
                If dtbBill.Rows.Count > 0 Then
                    For intCount As Integer = 0 To dtbBill.Rows.Count - 1
                        lblBalDisplay.Text = lblBalDisplay.Text + dtbBill.Rows(intCount).Item("Balance Amt")
                    Next
                    btnBills.Enabled = True
                    If ShowGrid Then Call Display_PendingBills() : Me.Cursor = Cursors.Default : Exit Sub
                End If
            Else
                btnBills.Enabled = False
                fgBills.Visible = False
            End If

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "Show_PendingBills")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub DisplayItems()
        Dim dtDItems As DataTable
        Try
            Me.Cursor = Cursors.WaitCursor
            'If ChkAll.Checked = True And cmDVendor.SelectedIndex <= 0 Then 'If vendor is not selected
            dtDItems = DItem.GetItemsPI()
            dtDItems.PrimaryKey = New DataColumn() {dtDItems.Columns("ITEMID")} 'Item ID

            'ElseIf ChkAll.Checked = True And cmDVendor.SelectedIndex > 0 Then 'If vendor & Check box is selected
            'dtDItems = DItem.GetItemsPI()
            'Else
            'dtDItems = DItem.GetvendorItems(cmDVendor.SelectedValue) 'DItem.GetItems(StrItems)
            'End If
            cmDItems.DataSource = dtDItems
            cmDItems.ValueMember = "ItemID"
            cmDItems.DisplayMember = "Name"
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisplayItems")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub CalcCashDiscount()
        Dim intCount As Integer
        Dim dblSchemeLess As Double
        Dim dblDiscLess As Double
        Dim dblItemTax As Double
        Dim dblFinalAmt As Double
        Dim DiscAmt As Double
        Dim TotalDiscAmt As Double
        Dim TotalTax As Double
        Dim FinalTotal As Double
        Dim TotSchmDisc As Double
        Dim Sale1 As Double
        Dim NetRate As Double
        Dim dblTotalAmut As Double
        Dim Scheme As CScheme
        Try
            With fgItems
                For intCount = 1 To .Rows.Count - 1
                    If IsNothing(.Item(intCount, 0)) OrElse Trim(.Item(intCount, 0)) = String.Empty Then Me.Cursor = Cursors.Default : Exit For
                    If IsNothing(.Item(intCount, 9)) OrElse Trim(.Item(intCount, 9)) = String.Empty Then Me.Cursor = Cursors.Default : Exit For
                    dblTotalAmut += .Item(intCount, 16)
                    dblSchemeLess = Math.Round(CDbl(.Item(intCount, 16)) - CDbl(.Item(intCount, 27)), 2)
                    DiscAmt = Math.Round((dblSchemeLess * CDbl(txtPerc.Text)) / 100, 2)
                    dblDiscLess = dblSchemeLess - DiscAmt
                    TotalDiscAmt += DiscAmt
                    TotSchmDisc += .Item(intCount, 27)
                    Dim objTax() As TaxPI

                    If Not IsNothing(.Item(intCount, 26)) Then
                        Dim tmpObj As Object = .Item(intCount, 26)
                        If tmpObj.Length > 0 Then
                            ReDim objTax(tmpObj.Length - 1)
                            For intInCount As Integer = 0 To tmpObj.Length - 1
                                objTax(intInCount) = CType(tmpObj(intInCount), TaxPI)
                                If Not IsNothing(objTax(intInCount)) Then
                                    If objTax(intInCount).TaxValue > 0 Then
                                        If Not (objTax(intInCount).ApplyTo = strSale OrElse objTax(intInCount).ApplyTo = strSalelExcl) Then
                                            dblItemTax += dblDiscLess * (objTax(intInCount).TaxValue / 100)
                                        End If
                                    End If
                                End If
                            Next
                        End If
                    End If
                    Scheme = CType(.Item(intCount, 8), CScheme)  'Scheme object

                    If Not IsNothing(Scheme) Then 'IF SHCME IS SELECTED
                        If Scheme.ItemID = Scheme.schemeItemID AndAlso Scheme.ApllyTo = "P" Then  'SCHEME IS FOR PURCHASE AND SAME ITEM IS FREE
                            If Not IsNothing(.Item(intCount, 24)) OrElse Trim(.Item(intCount, 0)) <> String.Empty Then   'IF SAME ITEM QUANTITY IS NOT PROVOIDED
                                NetRate = Math.Round((dblDiscLess + dblItemTax) / (CDbl(.Item(intCount, 9)) + CDbl(.Item(intCount, 24))), 2)  ' 9:-Qty

                            Else  'SAME QUANTITY IS PROVIDED
                                NetRate = Math.Round((dblDiscLess + dblItemTax) / .Item(intCount, 9), 2)  ' 9:-Qty
                            End If
                        Else  'SCHEME IS AVAILABLE AND  DIFF ITEM IS FREE 
                            NetRate = Math.Round((dblDiscLess + dblItemTax) / .Item(intCount, 9), 2)  ' 9:-Qty
                        End If
                    Else 'IF SCHEME IS NOT APPLIED
                        If Not IsNothing(.Item(intCount, 24)) OrElse Trim(.Item(intCount, 0)) <> String.Empty Then  'IF SAME ITEM QUANTITY IS ETERED WITHOUT SCHME
                            NetRate = Math.Round((dblDiscLess + dblItemTax) / (CDbl(.Item(intCount, 9)) + CDbl(.Item(intCount, 24))), 2)  ' 9:-Qty
                        Else  'NO FREE  QUANTITY IS APPLIED
                            NetRate = Math.Round((dblDiscLess + dblItemTax) / .Item(intCount, 9), 2)  ' 9:-Qty
                        End If
                    End If

                    .Item(intCount, 21) = NetRate

                    Sale1 = DPurchaseInvoice.GetMargin(.Item(intCount, 0)) * NetRate
                    If Sale1 > 0 Then
                        Sale1 += NetRate
                        If Sale1 > .Item(intCount, 12) Then
                            .Item(intCount, 17) = .Item(intCount, 12) '12 MRP   'IF CALCULATED SALE RATE GRATER THEN MRP THEN SET MRP AS SALE RATE
                        Else
                            .Item(intCount, 17) = Sale1
                        End If
                    Else
                        .Item(intCount, 17) = .Item(intCount, 12)
                    End If

                    'If Sale1 > .Item(intCount, 12) Then  '12 MRP   'IF CALCULATED SALE RATE GRATER THEN MRP THEN SET MRP AS SALE RATE
                    '    .Item(intCount, 17) = .Item(intCount, 12)
                    'Else
                    '    .Item(intCount, 17) = Sale1
                    'End If


                    TotalTax = TotalTax + dblItemTax
                    dblFinalAmt = dblDiscLess + dblItemTax

                    FinalTotal = FinalTotal + dblFinalAmt
                    dblDiscLess = 0
                    dblFinalAmt = 0
                    dblSchemeLess = 0
                    dblItemTax = 0
                    Sale1 = 0
                    NetRate = 0
                Next
                txtSubTotDisply.Text = BCommon.FormatAmount(Math.Round(dblTotalAmut, 2))
                txtTax.Text = BCommon.FormatAmount(Math.Round(TotalTax, 2))
                txtSchmDisc.Text = BCommon.FormatAmount(Math.Round(TotSchmDisc, 2))
                txtGrandTotal.Text = BCommon.FormatAmount(Math.Round(FinalTotal, 2))
                TextBox1.Text = BCommon.FormatAmount(Math.Round(txtGrandTotal.Text - txtPaidAmt.Text, 2))

            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "CalcCashDiscount")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Function isUpdate() As Boolean
        Try
            If cCmbDcNo.SelectedValue <> cCmbDcNo.Tag _
            Or Trim(cmbVendorID.Text) <> cmbVendorID.Tag _
            Or Trim(txtRemarks.Text) <> txtRemarks.Tag Or ccmbBankname.SelectedValue <> ccmbBankname.Tag Or ccmbCardtype.SelectedValue <> ccmbCardtype.Tag Then
                'Or cmDItems.SelectedValue <> cmDItems.Tag Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "isUpdate")
            Me.Cursor = Cursors.Default
        End Try
    End Function
    Private Function CalcDiscAmount(Optional ByVal DiscType As String = "") As Double
        Dim intNewAmount As Double
        Dim intCount As Integer
        Try
            If Trim(txtPerc.Text) = String.Empty Then txtPerc.Text = "0.00"
            If Trim(txtDisc.Text) = String.Empty Then txtDisc.Text = "0.00"

            If DiscType = "A" Then
                If (CDbl(txtSubTotDisply.Text) - CDbl(txtSchmDisc.Text)) <= 0 Then
                    txtPerc.Text = 0
                Else
                    txtPerc.Text = BCommon.FormatAmount(Math.Round((CDbl(txtDisc.Text) * 100) / (CDbl(txtSubTotDisply.Text) - CDbl(txtSchmDisc.Text)), 2))
                End If

            ElseIf DiscType = "P" Then
                If (CDbl(txtSubTotDisply.Text) - CDbl(txtSchmDisc.Text)) <= 0 Then
                    txtDisc.Text = 0
                Else
                    txtDisc.Text = BCommon.FormatAmount(Math.Round((CDbl(txtSubTotDisply.Text) - CDbl(txtSchmDisc.Text)) * (CDbl(txtPerc.Text) / 100), 2))
                End If
            End If

            intNewAmount = 0
            With fgItems
                For intCount = 1 To .Rows.Count - 1
                    intNewAmount = intNewAmount + .Item(intCount, 16)
                Next
            End With
            If Trim(txtPerc.Text) = String.Empty Then txtPerc.Text = "0.00"
            If Trim(txtDisc.Text) = String.Empty Then txtDisc.Text = "0.00"

            Call CalcCashDiscount()

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "CalcDiscAmount")
            Me.Cursor = Cursors.Default
        Finally
            CalcDiscAmount = intNewAmount
        End Try
    End Function

    Private Sub showProductList(ByVal intColumnIndex As Integer, Optional ByVal keyChar As Char = "")
        Dim intSelectedProduct As Integer

        Dim btContinue As Byte = 0
        Try

            With fgItems
                If IsNothing(.Item(.Row, "STATUS")) Then
                    If .Row = 0 Then
                        Exit Sub
                    ElseIf .Row = 1 Then
                        btContinue = 1
                    ElseIf Not IsNothing(.Item(.Row - 1, ItemGridColumns.ITEMID)) OrElse Trim(.Item(.Row - 1, ItemGridColumns.ITEMID)) <> "" Then
                        btContinue = 1
                    End If
                ElseIf .Item(.Row, "STATUS") = "" OrElse .Item(.Row, "STATUS") = "A" Then 'ITEM SHOULD BE UPDATE ONLY IF ITEM IS ACTIVE NOT FOR :-.ITEM(.ROW, "STATUS") = "U" ORELSE .ITEM(.ROW, "STATUS") = "W" 
                    btContinue = 1
                End If
                If btContinue = 0 Then Exit Sub
            End With

            Dim frmListProducts As New frmProducts
            With fgItems

                frmListProducts.Height = .Height - .Rows.DefaultSize - 2
                frmListProducts.Width = .Width - .Cols("ITEMNAME").Width - .Cols(ItemGridColumns.CLRCODE).Width - 15

                frmListProducts.Left = fgItems.Cols(ItemGridColumns.CLRCODE).Left + fgItems.Left + Me.Left + 5
                frmListProducts.Top = fgItems.Rows(0).Bottom + fgItems.Top + Me.Top + Me.Parent.Top + 5
                frmListProducts.intSelectedItem = 0

                ' frmListProducts.intColumnIndex = intColumnIndex
                ' frmListProducts.intSearchFor = 1
                'frmListProducts.keyChar = keyChar
                If .Item(.Row, intColumnIndex) <> Nothing Then
                    If Not IsNothing(.Item(.Row, ItemGridColumns.ITEMID)) AndAlso Trim(.Item(.Row, ItemGridColumns.ITEMID)) Then frmProducts.intSelectedItem = .Item(.Row, ItemGridColumns.ITEMID)
                End If

                frmListProducts.ShowDialog()
                intSelectedProduct = frmListProducts.intSelectedItem
            End With

            If intSelectedProduct > 0 Then

                '   If fgItems.Item(fgItems.Row, intColumnIndex) = Nothing OrElse fgItems.Item(fgItems.Row, intColumnIndex).ToString() = "" Then intCrntItmCnt += 1
                btLoading = 0
                With frmListProducts.fgProducts
                    If .CurrentRow.Index > 0 Then

                        intCurrentRow = fgItems.Row

                        fgItems.Item(fgItems.Row, ItemGridColumns.ITEMID) = .Item(.CurrentRow.Index, 0)
                        fgItems.Item(fgItems.Row, "CODE") = .Item(.CurrentRow.Index, 1)
                        fgItems.Item(fgItems.Row, "ITEMNAME") = .Item(.CurrentRow.Index, 2)

                        fgItems.Item(fgItems.Row, "QTY") = "1"
                        fgItems.Item(fgItems.Row, ItemGridColumns.TAX) = "0.00"
                        fgItems.Item(fgItems.Row, ItemGridColumns.RATE) = "0.00"
                        fgItems.Item(fgItems.Row, ItemGridColumns.AMOUNT) = "0.00"

                        intCurrentRow = fgItems.Row
                        btLoading = 1
                        fgItems.Col = fgItems.Cols(ItemGridColumns.CLRCODE).Index

                        Call CalcTotalAmount()
                    End If
                End With
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "showProductList")
        Finally
            btLoading = 0
        End Try
    End Sub

    Private Sub showColorList(ByVal intColumnIndex As Integer, Optional ByVal keyChar As Char = "")
        Dim intSelectedProduct As Integer

        Dim btContinue As Byte = 0
        Try
            Dim frmListProducts As New frmProducts

            With fgItems
                If IsNothing(.Item(.Row, ItemGridColumns.ITEMID)) Then
                    Exit Sub
                ElseIf Trim(.Item(.Row, ItemGridColumns.ITEMID)) = "" Then
                    Exit Sub

                End If


                frmListProducts.Height = .Height - .Rows.DefaultSize - 2
                frmListProducts.Width = .Width - .Cols("ITEMNAME").Width - .Cols(ItemGridColumns.CLRCODE).Width - 15


                frmListProducts.Left = fgItems.Cols(ItemGridColumns.CLRCODE).Left + fgItems.Left + Me.Left + 5
                frmListProducts.Top = fgItems.Rows(0).Bottom + fgItems.Top + Me.Top + Me.Parent.Top + 5
                frmListProducts.intSelectedItem = 0

                'frmListProducts.intColumnIndex = 2
                '  frmListProducts.intDisplayIndex = 2
                frmListProducts.intProductID = .Item(.Row, ItemGridColumns.ITEMID)
                'frmListProducts.keyChar = keyChar
                If .Item(.Row, intColumnIndex) <> Nothing Then
                    If Not IsNothing(.Item(.Row, "COLORID")) AndAlso Trim(.Item(.Row, "COLORID")) Then frmProducts.intSelectedItem = .Item(.Row, "COLORID")
                End If


                frmListProducts.ShowDialog()
                intSelectedProduct = frmListProducts.intSelectedItem
            End With

            If intSelectedProduct > 0 Then

                'If fgItems.Item(fgItems.Row, intColumnIndex) = Nothing OrElse fgItems.Item(fgItems.Row, intColumnIndex).ToString() = "" Then intCrntItmCnt += 1
                btLoading = 0
                With frmListProducts.fgProducts
                    If .CurrentRow.Index > 0 Then

                        intCurrentRow = fgItems.Row
                        btLoading = 1
                        fgItems.Item(fgItems.Row, ItemGridColumns.CLRID) = .Item(.CurrentRow.Index, 0)
                        fgItems.Item(fgItems.Row, ItemGridColumns.CLRCODE) = .Item(.CurrentRow.Index, 1)
                        fgItems.Item(fgItems.Row, ItemGridColumns.CLRDESC) = .Item(.CurrentRow.Index, 2)
                        intCurrentRow = fgItems.Row
                        fgItems.Col = fgItems.Cols(ItemGridColumns.PACK).Index
                        Call CalcTotalAmount()
                        btLoading = 0
                    End If
                End With
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "showColorList")
        Finally
            btLoading = 0
        End Try
    End Sub
#End Region

#Region "Grid Settings"

    Private Sub Grid_Settings()
        Try
            fgItems.Clear(C1.Win.C1FlexGrid.ClearFlags.All)
            Call SetGridCaptions()
            Call SetColumnWidths()
            With fgItems
                .Cols(2).Editor = cmDItems
                .SelectionMode = C1.Win.C1FlexGrid.SelectionModeEnum.Row
                If _strVoucherType <> "DP" Then .AllowDelete = True
                .ScrollBars = ScrollBars.None
                For count As Byte = 0 To .Cols.Count - 1
                    .Cols(count).Visible = False
                Next

                .Cols("CODE").Visible = True

                .Cols("ITEMNAME").Visible = True
                .Cols(ItemGridColumns.CLRCODE).Visible = True
                .Cols("CLRDESC").Visible = True
                .Cols("PACK").Visible = True
                .Cols("GODOWN").Visible = True
                .Cols("SCHEME").Visible = True
                .Cols("QTY").Visible = True
                .Cols("RATE").Visible = True
                .Cols("TAX").Visible = True
                .Cols("AMOUNT").Visible = True


                'NON EDITABLE COLUMNS
                .Cols("AMOUNT").AllowEditing = False   'ITEM ID


                'DISPLAY FORMAT   
               
                .Cols("QTY").DataType = System.Type.GetType("System.Int32")
                .Cols("RATE").DataType = System.Type.GetType("System.Text")
                .Cols("AMOUNT").DataType = System.Type.GetType("System.Decimal")
                .Cols("TAX").DataType = System.Type.GetType("System.Decimal")


                .Cols("RATE").TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.RightCenter
                .Cols("AMOUNT").TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.RightCenter
                .Cols("TAX").TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.RightCenter

                .Cols("QTY").Format = "0.00"
                .Cols("RATE").Format = "0.00"
                .Cols("AMOUNT").Format = "0.00"
                .Cols("TAX").Format = "0.00"
                .Cols(ItemGridColumns.PACK).Editor = CmbPack
            End With

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "Grid_Settings")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub SetGridCaptions()
        Try
            With fgItems
                .Cols.Count = 28
                .Rows.Count = intRowCount
                .Rows.Fixed = 1
                'SET COLUMN NAMES 
                .Cols(0).Name = "ITEMID"
                .Cols(1).Name = "CODE"
                .Cols(2).Name = "ITEMNAME"

                .Cols(3).Name = "CLRCODE"
                .Cols(4).Name = "CLRDESC"
                .Cols(5).Name = "PACKID"
                .Cols(6).Name = "PACK"
                .Cols(7).Name = "GODOWNID"
                .Cols(8).Name = "GODOWN"

                .Cols(9).Name = "SCHEMEID"
                .Cols(10).Name = "SCHEME"
                .Cols(11).Name = "QTY"

                .Cols(12).Name = "RATE"

                .Cols(14).Name = "TAXID"
                .Cols(15).Name = "TAX"

                .Cols(16).Name = "AMOUNT"
                .Cols(17).Name = "STATUS"
                .Cols(18).Name = "COLORID"


                'CAPTION

                .Cols(ItemGridColumns.ITEMID).Caption = ItemGridColumns.ITEMID
                .Cols("CODE").Caption = "CODE"
                .Cols("ITEMNAME").Caption = "NAME"
                .Cols(ItemGridColumns.CLRCODE).Caption = "CLR CODE"
                .Cols("CLRDESC").Caption = "CLR DESC"

                .Cols("PACKID").Caption = "PACKID"
                .Cols("PACK").Caption = "PACK"
                .Cols("GODOWNID").Caption = "GODOWNID"
                .Cols("GODOWN").Caption = "GODOWN"

                .Cols("SCHEMEID").Caption = "SchemeID"  'CSCHEME
                .Cols("SCHEME").Caption = "SCHEME"

                .Cols("QTY").Caption = "QTY"

                '.Cols("MRP").Caption = "MRP"
                .Cols("RATE").Caption = "RATE"
                .Cols("TAXID").Caption = "Tax ID"
                .Cols("TAX").Caption = "TAX"
                .Cols("AMOUNT").Caption = "AMOUNT"
                .Cols("STATUS").Caption = "STATUS"
                .Cols("COLORID").Caption = "COLORID"

                '.Cols(19).Caption = "VoucherNO"
                '.Cols(20).Caption = "SchemeAmt"
                '.Cols(21).Caption = "Net_Price"
                '.Cols(22).Caption = "Lowest_Price"
                '.Cols(23).Caption = "SchemeDisc"
                '.Cols(24).Caption = "DiscType"
                '.Cols(25).Caption = "DiscAmount"
                '.Cols(26).Caption = "ObjPI"
                '.Cols(27).Caption = "TotalDisc"
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetGridCaptions")
            Me.Cursor = Cursors.Default
        End Try

    End Sub

    Private Sub SetColumnWidths()

        Dim intGridWidth As Integer = fgItems.Width - 5
        Try
            With fgItems
                For count As Byte = 0 To .Cols.Count - 1
                    .Cols(count).Width = 0
                Next
                .Cols("CODE").Width = intGridWidth / 13           '"ITEM CODE"
                .Cols("ITEMNAME").Width = intGridWidth / 5       '"ITEM NAME"
                .Cols(ItemGridColumns.CLRCODE).Width = intGridWidth / 12           '"CLRCODE"
                .Cols("CLRDESC").Width = intGridWidth / 10           '"CLRDESC"
                .Cols("PACK").Width = intGridWidth / 12           '"PACK"
                .Cols("GODOWN").Width = intGridWidth / 13           '"GODOWN"
                .Cols("SCHEME").Width = intGridWidth / 13           '"SCHEME"
                .Cols("QTY").Width = intGridWidth / 15          '"SCHEME QTY"  'SCHEME DISPLAY 

                .Cols("TAX").Width = intGridWidth / 13          'QTY
                .Cols("RATE").Width = intGridWidth / 12        '"TAX"
                .Cols("AMOUNT").Width = intGridWidth / 14          '"RATE"                        

            End With

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetColumnWidths")
            Me.Cursor = Cursors.Default
        End Try

    End Sub

#End Region

#Region "GRID EVENTS"

    Private Sub fgItems_AfterDeleteRow(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles fgItems.AfterDeleteRow
        btDelete = 0
        CalculateAmt()

        If fgItems.Rows.Count < intRowCount Then fgItems.Rows.Count += 10
        CalcCashDiscount()
    End Sub

    Private Sub fgItems_AfterRowColChange(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RangeEventArgs) Handles fgItems.AfterRowColChange
        If btLoading = 1 Then Me.Cursor = Cursors.Default : Exit Sub
        Try
            With fgItems
                If intCurrentRow >= .Row Then Me.Cursor = Cursors.Default : Exit Sub ' Row is not changed '1
                If IsNothing(.Item(intCurrentRow, 0)) OrElse Trim(.Item(intCurrentRow, 0)) = "" Then
                    e.Cancel = True
                    .Row = intCurrentRow
                    .Col = 1
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgItems_AfterRowColChange")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub fgItems_BeforeDeleteRow(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles fgItems.BeforeDeleteRow
        Try
            If btDelete = 0 Then
                If IsNothing(fgItems(e.Row, 0)) OrElse Trim(fgItems(e.Row, 0)) = String.Empty Then e.Cancel = True
                ' If BCommon.ShowMsgError(10084, "YN") = Windows.Forms.DialogResult.Yes Then
                btDelete = 1
                'Else
                '   e.Cancel = True
                ' End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgItems_BeforeDeleteRow")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub fgItems_BeforeEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles fgItems.BeforeEdit
        If btLoading = 1 Then Me.Cursor = Cursors.Default : Exit Sub
        With fgItems
            If .Col > ItemGridColumns.ITEMNAME Then
                If .Item(.Row, 0) = 0 Then e.Cancel = True
            Else
                If e.Row > 1 AndAlso (IsNothing(.Item(e.Row - 1, 0)) OrElse Trim(.Item(e.Row - 1, 0)) = String.Empty) Then e.Cancel = True 'PREVIOUS ROW IS FIELD
            End If
        End With
    End Sub

    Private Sub fgItems_BeforeRowColChange(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RangeEventArgs) Handles fgItems.BeforeRowColChange
        If btLoading = 1 Then Me.Cursor = Cursors.Default : Exit Sub
        Try
            If e.OldRange.r1 <> e.NewRange.r1 Then
                'Checks is previous item details filled completely
                Dim intBlankColumn As Integer
                intBlankColumn = isValidRow(e.OldRange.r1)
                If intBlankColumn > 0 Then
                    fgItems.Col = intBlankColumn
                    e.Cancel = True
                End If
                'End If
            End If

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgItems_BeforeRowColChange")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub fgItems_AfterEdit(sender As Object, e As C1.Win.C1FlexGrid.RowColEventArgs) Handles fgItems.AfterEdit
        With fgItems

            Select Case .Col
                Case .Cols(ItemGridColumns.QTY).Index
                    CalcTotalAmount()
                Case .Cols(ItemGridColumns.RATE).Index
                    CalcTotalAmount()
                Case .Cols(ItemGridColumns.TAX).Index
                    CalcTotalAmount()
            End Select
        End With

    End Sub



    Private Sub fgItems_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles fgItems.KeyDown

        If btLoading = 1 Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Left OrElse e.KeyCode = Keys.Right OrElse e.KeyCode = Keys.Down OrElse e.KeyCode = Keys.Up OrElse e.KeyCode = Keys.PageDown Then Exit Sub
        Try
            With fgItems
                'If e.KeyCode = Keys.Enter Then
                '    If _strVoucherType = "DP" And .Col = 2 Then
                '        If IsNothing(.Item(.Row, .Col)) Then Me.Cursor = Cursors.Default : Exit Sub
                '        Call SetItemRow("A", .Row, False)
                '        If Not IsNothing(.Item(.Row, .Col)) Or .Item(.Row, .Col) <> "" Then
                '            btnFreeze.Enabled = True
                '            btnUnFreeze.Enabled = True
                '        End If
                '    End If
                'Else
                If fgItems.Col = fgItems.Cols("CODE").Index Or fgItems.Col = fgItems.Cols("ITEMNAME").Index Then
                    Call showProductList(fgItems.Col)
                    Call CheckSetItemRow(.Row)
                ElseIf fgItems.Col = ItemGridColumns.CLRCODE Or fgItems.Col = ItemGridColumns.CLRDESC Then
                    Call showColorList(fgItems.Col)
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgItems_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub fgItems_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If btLoading = 1 Then Me.Cursor = Cursors.Default : Exit Sub
        Try
            With fgItems
                If _strVoucherType = "DP" And .Col = 2 Then
                    If IsNothing(.Item(.Row, .Col)) Then Me.Cursor = Cursors.Default : Exit Sub
                    ' Call SetItemRow("A", .Row, False)
                    If Not IsNothing(.Item(.Row, .Col)) Or .Item(.Row, .Col) <> "" Then
                        btnFreeze.Enabled = True
                        btnUnFreeze.Enabled = True
                    End If
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgItems_MouseDoubleClick")
        End Try
    End Sub
    Private Sub frmSale_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        Dim intClickCnt As Integer = 0
        Try
            If e.Control = True Then
                If e.Alt = True Then
                    'If btShortCuts = 1 Then Exit Sub
                    Select Case e.KeyCode
                        Case Keys.F         'FREEZE SELECTED ITEMS
                            ' Call FreezeItems("A")
                        Case Else
                            'Check for 48 to 57 (0-9) And 96 to 105(NUMPAD (0-9)
                            If (e.KeyCode > 48 AndAlso e.KeyCode < 58) OrElse (e.KeyCode > 96 AndAlso e.KeyCode < 106) Then
                                If e.KeyCode < 58 Then
                                    intClickCnt = e.KeyCode - 48
                                Else : intClickCnt = e.KeyCode - 96
                                End If
                            End If
                    End Select
                Else

                End If
            ElseIf e.KeyCode = Keys.Escape Then 'HIDE SHORTCUTS OR COUNTER SUMMARY WINDOW
                'DisplayShortCuts("H")
                'btShortCuts = 0
            ElseIf e.KeyCode = Keys.F1 Then
                e.SuppressKeyPress = True
                ''Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Sale")
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmSale_KeyUp")
        Finally
            intClickCnt = Nothing
        End Try
    End Sub
    Private Sub fgItems_EnterCell(ByVal sender As Object, ByVal e As System.EventArgs) Handles fgItems.EnterCell
        If btLoading = 1 Then Exit Sub
        ' MessageBox.Show("sdfsdf", "CAP", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        Try
            With fgItems
                If intCurrentRow = 0 Then Exit Sub
                If .RowSel < 0 OrElse .Row < 0 Then Exit Sub
                If .RowSel > 1 Then
                    If .Item(.RowSel - 1, ItemGridColumns.ITEMID) = Nothing OrElse .Item(.RowSel - 1, ItemGridColumns.ITEMID).ToString() = "" Then Exit Sub
                End If

                If .Rows(.Row).AllowEditing = False Then Exit Sub
                If .RowSel = .Rows.Count - 1 Then .Rows.Count = .Rows.Count + 10 'INCREMENTS NO OF ROWS BY 10 IF REACHED TO LAST ROW 
                Select Case .Col
                    Case .Cols("ITEMNAME").Index
                        If IsNothing(.Item(.Row, .Col)) OrElse Trim(.Item(.Row, .Col)) = "" Then Call showProductList(.Col)

                    Case ItemGridColumns.CLRCODE, ItemGridColumns.CLRDESC
                        If IsNothing(.Item(.Row, .Col)) OrElse Trim(.Item(.Row, .Col)) = "" Then Call showColorList(.Col)
                    Case ItemGridColumns.PACK
                        If Not (.Item(.RowSel, ItemGridColumns.ITEMID) = Nothing OrElse .Item(.RowSel, ItemGridColumns.ITEMID).ToString() = "") Then
                            'If IsNothing(.Item(.RowSel, "PACK")) OrElse Trim(.Item(.RowSel, "PACK")) = "" Then showBatchDetails()
                        End If
                End Select
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgItems_EnterCell")
        End Try
    End Sub
    Private Sub fgItems_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles fgItems.Click, fgItems.DoubleClick
        Try
            With fgItems
                If .Cols("CODE").Index = .MouseCol Or .Cols("ITEMNAME").Index = .MouseCol Then
                    showProductList(.MouseCol)
                ElseIf .Cols(ItemGridColumns.CLRCODE).Index = .MouseCol Or .Cols(ItemGridColumns.CLRDESC).Index = .MouseCol Then
                    showColorList(.MouseCol)
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgItems_Click")
        End Try
    End Sub
    Private Sub fgItems_SetupEditor(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles fgItems.SetupEditor
        If btLoading = 1 Then Me.Cursor = Cursors.Default : Exit Sub
        'Dim ctl As Control = fgItems.Editor
        With fgItems
            Select Case .Col
                Case 5  'BATCH
                    Dim txt As TextBox = DirectCast(fgItems.Editor, TextBox)
                    txt.MaxLength = 20
                Case 9, 10  'QTY 'SCHEME
                    Dim txt As TextBox = DirectCast(fgItems.Editor, TextBox)
                    txt.MaxLength = 6
                    'Case 10 'SCHEME
                    '    CType(ctl, TextBox).MaxLength = 6
                Case 12, 13, 17 'MRP ,'RATE , 'SALE RATE
                    Dim txt As TextBox = DirectCast(fgItems.Editor, TextBox)
                    txt.MaxLength = 13
                    'Case 13 'RATE
                    '    CType(ctl, TextBox).MaxLength = 13
                    'Case 17 'SALE RATE
                    '    CType(ctl, TextBox).MaxLength = 13
            End Select
        End With
    End Sub

    Private Sub fgItems_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles fgItems.TextChanged
        If btLoading = 1 Then Me.Cursor = Cursors.Default : Exit Sub
        Dim intCount As Integer
        Dim dblAmount As Double
        Try
            With fgItems
                If .Col = 15 Then
                    For intCount = 1 To .Rows.Count - 1
                        If .Item(.Row, 15) <> Nothing AndAlso .Item(.Row, 15) <> "" Then
                            dblAmount = dblAmount + .Item(.Row, 15)
                        End If
                    Next
                    txtGrandTotal.Text = BCommon.FormatAmount(Math.Round(dblAmount, 2))
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgItems_TextChanged")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    'Private Sub fgItems_ValidateEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.ValidateEditEventArgs)
    '    If btLoading = 1 Then Me.Cursor = Cursors.Default : Exit Sub
    '    Dim intValue As Object
    '    Dim intCount As Integer, intItemID As Integer
    '    Dim StrBatchNo As String = ""
    '    Dim dtbTempPurchase As New DataTable
    '    'Dim flg As Byte = 0
    '    Try
    '        With fgItems
    '            Select Case .Col
    '                Case 5 'Item entry with same ItemID & CLRcode not allowed  Vrushali..02/12/2007
    '                    If Not IsNothing(.Item(.Row, 0)) AndAlso .Item(.Row, 0) <> 0 AndAlso sender.editor.text() <> "" Then
    '                        intItemID = .Item(.Row, 0)
    '                        StrBatchNo = sender.editor.text()
    '                    End If
    '                    If .Row > 0 AndAlso intItemID > 0 Then
    '                        For intCount = 1 To .Rows.Count - 1
    '                            '.Rows(.Row).Clear(C1.Win.C1FlexGrid.ClearFlags.Content)
    '                            If .Row <> intCount Then
    '                                If .Item(intCount, 0) = intItemID And .Item(intCount, 5) = StrBatchNo Then
    '                                    BCommon.ShowMsgError(10268)
    '                                    e.Cancel = True
    '                                End If
    '                            End If
    '                        Next
    '                    End If
    '                Case 10      'Scheme
    '                    If sender.editor.text() <> "" Then
    '                        intValue = sender.editor.text()
    '                        If IsNumeric(intValue) <> True Then
    '                            BCommon.ShowMsgError(10239)
    '                            e.Cancel = True
    '                        End If
    '                    Else
    '                        sender.editor.text = 0
    '                    End If
    '                Case 9     'Quantity
    '                    '.Item(.Row, 5)="CLRcode"
    '                    If .Item(.Row, 5) <> "" And Not IsNothing(.Item(.Row, 0)) And .Item(.Row, 0) <> 0 Then
    '                        dtbTempPurchase = DPurchaseInvoice.GetTempPurItems(.Item(.Row, 0), .Item(.Row, 5), cmDVendor.SelectedValue)
    '                    End If
    '                    'If Not IsNothing(dtbTempPurchase) Or dtbTempPurchase.Rows.Count > 0 Then
    '                    If Not IsNothing(.Item(e.Row, 9)) And Trim(.Item(e.Row, 9)) <> "" Then
    '                        If Trim(fgItems.Editor.Text) <= "0" Or fgItems.Editor.Text = "" Then
    '                            BCommon.ShowMsgError(10232)
    '                            e.Cancel = True
    '                            'Quantity should be greater than temparary (already) Purchased Items  Vrushali..02/01/2007  
    '                        ElseIf Not IsNothing(dtbTempPurchase) Or dtbTempPurchase.Rows.Count > 0 Then
    '                            If dtbTempPurchase.Rows(0).Item(4) > Trim(fgItems.Editor.Text) Then
    '                                '
    '                                MessageBox.Show("Quantity should be greater than " & dtbTempPurchase.Rows(0).Item(4) & "", ConfigurationManager.AppSettings("MsgTitle"), MessageBoxButtons.OK, MessageBoxIcon.Information)
    '                                e.Cancel = True
    '                            End If
    '                        End If
    '                    End If
    '                    'End If
    '                Case 12     'MRP
    '                    If Not IsNothing(.Item(e.Row, 12)) And Trim(.Item(e.Row, 12)) <> "" Then
    '                        If IsNumeric(fgItems.Editor.Text) Then
    '                            If fgItems.Editor.Text < 0 Then
    '                                BCommon.ShowMsgError(10311)
    '                                e.Cancel = True         'Put cursor on same column
    '                            End If
    '                        ElseIf fgItems.Editor.Text = "" Then
    '                            .Item(e.Row, 12) = "0.00"
    '                        Else
    '                            BCommon.ShowMsgError(10239)
    '                            e.Cancel = True
    '                        End If

    '                    End If
    '                Case 13     'Bill Rate
    '                    If Not IsNothing(.Item(e.Row, 13)) And Trim(.Item(e.Row, 13)) <> "" Then
    '                        If IsNumeric(fgItems.Editor.Text) Then
    '                            If fgItems.Editor.Text > .Item(e.Row, 12) Then

    '                                BCommon.ShowMsgError(10284)
    '                                e.Cancel = True         'Put cursor on same column
    '                            ElseIf fgItems.Editor.Text < 0 Then
    '                                BCommon.ShowMsgError(10394)
    '                                e.Cancel = True         'Put cursor on same column
    '                            End If
    '                        ElseIf fgItems.Editor.Text = "" Then
    '                            .Item(e.Row, 13) = "0.00"
    '                        Else
    '                            BCommon.ShowMsgError(10239)
    '                            e.Cancel = True
    '                        End If
    '                    End If
    '                Case 1
    '                    If Trim(.Editor.Text) = "" Then Me.Cursor = Cursors.Default : Exit Sub
    '                    If IsVendorSelected() = 0 Then
    '                        .Editor.Text = ""
    '                        e.Cancel = True
    '                        cmDVendor.Focus()
    '                        Me.Cursor = Cursors.Default
    '                        Exit Sub
    '                    End If
    '                    If DItem.GetItemID(.Editor.Text) > 0 Then
    '                        Dim ItemID As Integer = DItem.GetItemID(.Editor.Text)
    '                        If DItem.GetItemDtls(ItemID, cmDVendor.SelectedValue, True).Rows.Count = 0 Then
    '                            BCommon.ShowMsgError(10332)
    '                            .Editor.Text = ""
    '                            e.Cancel = True
    '                            Me.Cursor = Cursors.Default
    '                            Exit Sub
    '                        End If

    '                        If Not IsNothing(.Item(.Row, 0)) AndAlso Trim(.Item(.Row, 0)) <> "" Then
    '                            .Item(.Row, 0) = ItemID
    '                            .Item(.Row, 1) = .Editor.Text
    '                            Call SetItemRow("A", .Row, True)
    '                        ElseIf .Item(.Row, 0) = ItemID Then
    '                            Call SetItemRow("A", .Row, False)
    '                        Else
    '                            .Item(.Row, 0) = ItemID
    '                            Call SetItemRow("A", .Row, True)
    '                        End If
    '                    Else
    '                        BCommon.ShowMsgError(10224)
    '                        .Editor.Text = ""
    '                        e.Cancel = True
    '                        Me.Cursor = Cursors.Default
    '                        Exit Sub
    '                    End If
    '                Case 2
    '                    If Trim(.Editor.Text) = "" Then Me.Cursor = Cursors.Default : Exit Sub
    '                    If IsVendorSelected() = 0 Then
    '                        .Editor.Text = ""
    '                        e.Cancel = True
    '                        cmDVendor.Focus()
    '                        Me.Cursor = Cursors.Default
    '                        Exit Sub
    '                    End If

    '                    'HERE EDITOR IS ITEMNAME COMBO
    '                    If cmDItems.SelectedValue > 0 Then
    '                        If Not IsNothing(.Item(.Row, 0)) AndAlso Trim(.Item(.Row, 0)) <> "" Then
    '                            If .Item(.Row, 0) = cmDItems.SelectedValue Then
    '                                Call SetItemRow("A", .Row, False)
    '                                .Item(.Row, 0) = cmDItems.SelectedValue
    '                            Else
    '                                .Item(.Row, 0) = cmDItems.SelectedValue
    '                                Call SetItemRow("A", .Row, True)
    '                            End If
    '                        Else
    '                            .Item(.Row, 0) = cmDItems.SelectedValue
    '                            Call SetItemRow("A", .Row, True)
    '                        End If
    '                    Else

    '                    End If
    '            End Select

    '        End With
    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgItems_ValidateEdit")
    '        Me.Cursor = Cursors.Default
    '    End Try
    'End Sub

#End Region

#Region "TextBox Evemts"

    Private Sub txtDcNo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDcNo.KeyDown
        If e.Shift Then e.SuppressKeyPress = True : Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            txtBillNo.Focus() 'If _strVoucherType = "DC" Then txtRemarks.Focus() Else txtBillNo.Focus()
        End If
        If BCommon.ValidateMiscell(e.KeyCode, "USERID") = False Then
            e.SuppressKeyPress = True
        End If
    End Sub

    Private Sub txtTax_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTax.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If BCommon.ValidateText(e.KeyCode, "NUM", False) = False Then
            e.SuppressKeyPress = True
        End If
    End Sub

    Private Sub txtRemarks_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtRemarks.KeyDown
        If e.KeyCode = Keys.Enter Then
            txtPerc.Focus()
        Else
            If BCommon.ValidateText(e.KeyCode, "ALFTEXT", False) = False Then
                e.SuppressKeyPress = True
            End If
        End If
    End Sub

    Private Sub txtVoucherNo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtVoucherNo.KeyDown
        If e.KeyCode = Keys.Enter Then
            dtpDate.Focus()
        Else
            If BCommon.ValidateText(e.KeyCode, "ALFTEXT", False) = False Then
                e.SuppressKeyPress = True
            End If
        End If
    End Sub

    Private Sub txtBillNo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtBillNo.KeyDown
        If e.KeyCode = Keys.Enter Then dtpDate.Focus()
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.Shift = True Then
            If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                e.SuppressKeyPress = True : Me.Cursor = Cursors.Default : Exit Sub
            End If
        End If
        If BCommon.ValidateMiscell(e.KeyCode, "USERID") = False Then    'Divya 23/10/2007
            e.SuppressKeyPress = True
        End If
    End Sub

    Private Sub txtDisc_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDisc.KeyDown
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then SendKeys.Send("{TAB}")
            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If
            End If
            If BCommon.ValidateText(txtDisc.Text, e.KeyCode, "NUM") = False Then
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtDisc_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtPaidAmt_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPaidAmt.KeyDown
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then
                btnSave.Focus()
            Else
                If BCommon.ValidateText(txtPaidAmt.Text, e.KeyCode, "NUM") = False Then
                    e.SuppressKeyPress = True
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtPaidAmt_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtPerc_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPerc.KeyDown
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then SendKeys.Send("{TAB}")
            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If
            End If
            If BCommon.ValidateText(txtPerc.Text, e.KeyCode, "NUM") = False Then
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtPerc_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub ChkAll_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Try
            If e.KeyCode = Keys.Enter Then
                SendKeys.Send("{TAB}")
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ChkAll_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cmbVendorID_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then
                SendKeys.Send("{TAB}")
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmbVendorID_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub
    Private Sub txtTax_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTax.LostFocus
        Try
            txtTax.Text = BCommon.FormatAmount(Math.Round(CDbl(txtTax.Text), 2))
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtTax_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtDisc_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDisc.LostFocus
        Try
            Call CalcDiscAmount("A")
            Dim str = txtDisc.Text.ToString().Split(".")
            If UBound(str) <= 1 Then
                If txtDisc.Text <> "" Then
                    If CDbl(txtDisc.Text) > CDbl(Math.Round(CDbl(txtSubTotDisply.Text.Replace(",", "")) + CDbl(txtTax.Text.Replace(",", "")) - CDbl(txtSchmDisc.Text.Replace(",", "")), 2)) Then
                        MessageBox.Show(BCommon.GetMessage(10340).Replace("Amount", (CDbl(Math.Round(CDbl(txtSubTotDisply.Text) + CDbl(txtTax.Text.Replace(",", "")) - CDbl(txtSchmDisc.Text.Replace(",", "")), 2)))), ConfigurationManager.AppSettings("MsgTitle"), MessageBoxButtons.OK, MessageBoxIcon.Information)
                        txtDisc.Text = "0.00"
                        txtPerc.Text = "0.00"
                        txtGrandTotal.Text = BCommon.FormatAmount(Math.Round((((CDbl(txtSubTotDisply.Text.Replace(",", "")) - CDbl(txtSchmDisc.Text.Replace(",", ""))) - CDbl(txtDisc.Text.Replace(",", ""))) + CDbl(txtTax.Text.Replace(",", ""))), 2))
                        TextBox1.Text = txtGrandTotal.Text
                    Else
                        txtGrandTotal.Text = BCommon.FormatAmount(Math.Round((((CDbl(txtSubTotDisply.Text.Replace(",", "")) - CDbl(txtSchmDisc.Text.Replace(",", ""))) - CDbl(txtDisc.Text.Replace(",", ""))) + CDbl(txtTax.Text.Replace(",", ""))), 2))
                        TextBox1.Text = BCommon.FormatAmount(Math.Round((txtGrandTotal.Text - IIf(txtPaidAmt.Text = "", 0, txtPaidAmt.Text)), 2))
                        txtDisc.Text = BCommon.FormatAmount(Math.Round(CDbl(txtDisc.Text.Replace(",", "")), 2))
                    End If
                Else
                    txtDisc.Text = "0.00"
                End If
            Else
                txtDisc.Text = "0.00"
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtDisc_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtPaidAmt_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPaidAmt.LostFocus
        Try
            Dim str = txtPaidAmt.Text.ToString().Split(".")
            If UBound(str) <= 1 Then
                TextBox1.Text = BCommon.FormatAmount(Math.Round((txtGrandTotal.Text - IIf(txtPaidAmt.Text = "", 0, txtPaidAmt.Text)), 2))
                If txtPaidAmt.Text <> "" Then
                    If CDbl(txtPaidAmt.Text) > CDbl(txtGrandTotal.Text) Then
                        BCommon.ShowMsgError(10283)
                        txtPaidAmt.Text = "0.00"
                        txtPaidAmt.Focus()
                        TextBox1.Text = txtGrandTotal.Text
                    Else
                        txtPaidAmt.Text = BCommon.FormatAmount(Math.Round(CDbl(txtPaidAmt.Text.Replace(",", "")), 2))
                    End If
                Else
                    txtPaidAmt.Text = "0.00"
                End If
            Else
                txtPaidAmt.Text = "0.00"
                txtPaidAmt.Focus()
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtPaidAmt_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cmbVendorID_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim intVendId As Integer
        Dim dsVendorId As New DataSet
        Try
            If Me.cmbVendorID.Text <> "" Then
                dsVendorId = DVendor.GetVendorId(cmbVendorID.Text.ToUpper())
                If dsVendorId.Tables(0).Rows.Count > 0 Then
                    intVendId = Convert.ToInt32(dsVendorId.Tables(0).Rows(0).Item(0).ToString())
                    cmDVendor.SelectedValue = intVendId
                    'cmDVendor.Focus()
                Else
                    cmDVendor.SelectedValue = 0
                    BCommon.ShowMsgError(10219)
                    cmbVendorID.Text = ""
                    Me.cmbVendorID.Focus()
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmbVendorID_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtPerc_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPerc.LostFocus
        Try
            Call CalcDiscAmount("P")
            Dim str = txtDisc.Text.ToString().Split(".")
            If UBound(str) <= 1 Then
                If txtDisc.Text <> "" Then
                    If CDbl(txtDisc.Text) > CDbl(Math.Round(CDbl(txtSubTotDisply.Text.Replace(",", "")) + CDbl(txtTax.Text.Replace(",", "")) - CDbl(txtSchmDisc.Text.Replace(",", "")), 2)) Then
                        txtPerc.Text = "0.00"
                        txtPerc.Focus()
                        Exit Sub
                        txtDisc.Text = "0.00"
                        txtGrandTotal.Text = BCommon.FormatAmount(Math.Round((((CDbl(txtSubTotDisply.Text.Replace(",", "")) - CDbl(txtSchmDisc.Text.Replace(",", ""))) - CDbl(txtDisc.Text.Replace(",", ""))) + CDbl(txtTax.Text.Replace(",", ""))), 2))
                        TextBox1.Text = txtGrandTotal.Text
                    Else
                        txtGrandTotal.Text = BCommon.FormatAmount(Math.Round((((CDbl(txtSubTotDisply.Text.Replace(",", "")) - CDbl(txtSchmDisc.Text.Replace(",", ""))) - CDbl(txtDisc.Text.Replace(",", ""))) + CDbl(txtTax.Text.Replace(",", ""))), 2))
                        TextBox1.Text = BCommon.FormatAmount(Math.Round((txtGrandTotal.Text - IIf(txtPaidAmt.Text = "", 0, txtPaidAmt.Text)), 2))
                        txtDisc.Text = BCommon.FormatAmount(Math.Round(CDbl(txtDisc.Text.Replace(",", "")), 2))
                    End If
                Else : txtDisc.Text = "0.00"
                End If
            Else : txtDisc.Text = "0.00"
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtPerc_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtTax_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTax.TextChanged
        Try
            If txtDisc.Text = "" Then txtDisc.Text = "0.00"
            txtGrandTotal.Text = Format((((CDbl(txtSubTotDisply.Text) - CDbl(txtSchmDisc.Text)) - CDbl(txtDisc.Text)) + CDbl(txtTax.Text)), "0.00")
            txtDisc.Text = Format(CDbl(txtDisc.Text.Replace(",", "")), "0.00")
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtTax_TextChanged")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtGrandTotal_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtGrandTotal.TextChanged
        Try
            TextBox1.Text = BCommon.FormatAmount(Math.Round(txtGrandTotal.Text - Val(txtPaidAmt.Text), 2))
            txtPaidAmt.Text = BCommon.FormatAmount(Math.Round(Val(txtPaidAmt.Text), 2))
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtGrandTotal_TextChanged")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtSchmDisc_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSchmDisc.TextChanged
        Try
            If _strVoucherType = "PI" Or _strVoucherType = "DP" Then
                If txtDisc.Text = "" Then txtDisc.Text = "0.00"
                If txtSubTotDisply.Text = "" Then txtSubTotDisply.Text = "0.00"
                If txtTax.Text = "" Then txtTax.Text = "0.00"
                If txtSchmDisc.Text = "" Then txtSchmDisc.Text = "0.00"

                txtGrandTotal.Text = BCommon.FormatAmount(Math.Round((((CDbl(txtSubTotDisply.Text) - CDbl(txtSchmDisc.Text)) - CDbl(txtDisc.Text)) + CDbl(txtTax.Text)), 2))
                txtDisc.Text = BCommon.FormatAmount(Math.Round(CDbl(txtDisc.Text.Replace(",", "")), 2))
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtSchmDisc_TextChanged")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

#End Region

#Region "ComboBox/DropDown Events"

    Private Sub cmbPayMode_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbPayMode.KeyDown, ComboBox1.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then dtpDueDate.Focus()
    End Sub

    Private Sub cCmbDcNo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cCmbDcNo.KeyDown, C1Combo1.KeyDown
        If e.KeyCode = Keys.Enter Then dtpBillDate.Focus()
    End Sub

    Private Sub cmDVendor_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then
                If sender.selectedindex = -1 Then
                    sender.text = ""
                Else
                    Select Case _strVoucherType
                        Case "PI"
                            dtpBillDate.Focus()
                            'Case "DC"
                            '    txtDcNo.Focus()
                        Case "DP"
                            cCmbDcNo.Focus()
                    End Select
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmDVendor_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub dtpDate_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dtpDate.KeyDown
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then
                If dtpDate.Value > Date.Today Then
                    BCommon.ShowMsgError(10280)
                    dtpDate.Focus()
                Else
                    cmbVendorID.Focus()
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "dtpDate_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub dtpBillDate_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dtpBillDate.KeyDown
        If e.KeyCode = Keys.Enter Then
            If dtpBillDate.Value > Date.Today Then
                BCommon.ShowMsgError(10281)
                dtpBillDate.Focus()
            Else
                SendKeys.Send("{TAB}")
            End If
        End If
    End Sub

    Private Sub dtpDueDate_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dtpDueDate.KeyDown
        If e.KeyCode = Keys.Enter Then
            If dtpBillDate.Value > dtpDueDate.Value Then

                BCommon.ShowMsgError(10282)
                dtpDueDate.Focus()
            Else
                'If ChkAll.Enabled = True And btnBills.Enabled = True Then
                '    ChkAll.Focus()
                'Else
                fgItems.Focus()
                fgItems.Row = 1
                fgItems.Col = 1
                'End If
            End If

        End If
    End Sub

    Private Sub cmbPayMode_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPayMode.SelectedIndexChanged, ComboBox1.SelectedIndexChanged
        Try
            If cmbPayMode.SelectedIndex = 0 Then
                FGCheqDetails.Visible = False
                FGCardDetails.Visible = False
                ccmbBankname.Visible = False
                ccmbCardtype.Visible = False
            ElseIf cmbPayMode.SelectedIndex = 1 Then
                FGCheqDetails.Visible = False
                FGCardDetails.Visible = False
                ccmbBankname.Visible = False
                ccmbCardtype.Visible = False
            ElseIf cmbPayMode.SelectedIndex = 2 Then
                FGCheqDetails.Visible = False
                FGCardDetails.Visible = False
                ccmbBankname.Visible = False
                ccmbCardtype.Visible = False
            End If

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmbPayMode_SelectedIndexChanged")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cCmbDcNo_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cCmbDcNo.SelectedValueChanged, C1Combo1.SelectedValueChanged
        Dim dtDItems As DataTable
        Dim intCount As Integer
        Try
            If flagVendor = 0 Then Me.Cursor = Cursors.Default : Exit Sub

            With fgItems
                btLoading = 1
                .Rows.RemoveRange(1, .Rows.Count - 1)
                btLoading = 0

                dtDItems = DPurchaseInvoice.GetItems(cCmbDcNo.SelectedValue)
                If dtDItems.Rows.Count > 0 Then
                    btLoading = 1
                    .Rows.Count = dtDItems.Rows.Count + intRowCount - 2
                    btLoading = 0

                    For intCount = 0 To dtDItems.Rows.Count - 1
                        .Item(intCount + 1, ItemGridColumns.ITEMID) = dtDItems.Rows(intCount).Item(0)        'ITEM ID
                        .Item(intCount + 1, "CODE") = dtDItems.Rows(intCount).Item(1)        '"CODE"
                        .Item(intCount + 1, 2) = dtDItems.Rows(intCount).Item(2)        '"NAME"
                        .Item(intCount + 1, 3) = DItem.GetConversionFactor(dtDItems.Rows(intCount).Item(0))        '"PACKING"
                        .Item(intCount + 1, 4) = dtDItems.Rows(intCount).Item(3)        '"COMPANY"
                        .Item(intCount + 1, 5) = dtDItems.Rows(intCount).Item(5)        '"BATCH"
                        .Item(intCount + 1, 6) = dtDItems.Rows(intCount).Item(6)  '"MANUFACTURED"
                        .Item(intCount + 1, 7) = dtDItems.Rows(intCount).Item(7) '"EXPIRY"

                        If dtDItems.Rows(intCount).Item(8) > 0 Then
                            .Item(intCount + 1, 8) = BScheme.GetScheme(dtDItems.Rows(intCount).Item(8))       '"SCHEMEID"
                            .Item(intCount + 1, 10) = BScheme.GetSchemeName(.Item(intCount + 1, 8))
                        Else
                            .Item(intCount + 1, 8) = New CScheme
                            .Item(intCount + 1, 10) = dtDItems.Rows(intCount).Item(13)  'SCHEME QUANTITY
                        End If

                        .Item(intCount + 1, 9) = dtDItems.Rows(intCount).Item(9)       '"QUANTITY"
                        .Item(intCount + 1, 11) = " " + CStr(dtDItems.Rows(intCount).Item(15))       '"QUANTITY"

                        .Item(intCount + 1, 12) = dtDItems.Rows(intCount).Item(10)      '"M.R.P."
                        .Item(intCount + 1, 13) = dtDItems.Rows(intCount).Item(11)      '"BILL RATE"
                        .Item(intCount + 1, 14) = "0"                                     '"TAX ID"
                        .Item(intCount + 1, 15) = ""                                    '"TAX"
                        .Item(intCount + 1, 16) = dtDItems.Rows(intCount).Item(12)      '"AMOUNT"
                        .Item(intCount + 1, 17) = dtDItems.Rows(intCount).Item(16)      '"0.00"                                '"SALE RATE"
                        .Item(intCount + 1, 18) = "A"                                   '"STATUS"
                        .Item(intCount + 1, 19) = "0"                                   '"VOUCHERNO"
                        .Item(intCount + 1, 20) = dtDItems.Rows(intCount).Item(14)
                        .Item(intCount + 1, 24) = dtDItems.Rows(intCount).Item(13)      'SchemeQty
                    Next
                    Call CalcTotalAmount()
                    Call CalcCashDiscount()
                Else
                    BCommon.ShowMsgError(10082)
                    cCmbDcNo.Focus()
                End If
            End With

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cCmbDcNo_SelectedValueChanged")
            Me.Cursor = Cursors.Default
        Finally
            btLoading = 0
        End Try
    End Sub

    Private Sub cmDVendor_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            If IsNothing(cmDVendor.SelectedValue) OrElse cmDVendor.SelectedValue = 0 Then
                cmDVendor.Text = ""
                cmbVendorID.Text = ""
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmDVendor_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cmDVendor_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim strCreditDays As String
        If flagVendor = 0 Then Me.Cursor = Cursors.Default : Exit Sub
        Try
            If cmDVendor.SelectedValue > 0 Then
                ' ChkAll.Enabled = True      '**
                cmbVendorID.Text = ""
                cmbVendorID.Text = BCommon.GetSequentialCode("VENDOR") & Trim(cmDVendor.SelectedValue.ToString)
                fgItems.Cols(2).Editor = cmDItems
                BCommon.SetTodaysDate(dtpDueDate)
                strCreditDays = DVendor.GetCreditPeriod(cmDVendor.SelectedValue)
                strCreditDays = IIf(Trim(strCreditDays) = "", 0, strCreditDays)
                dtpDueDate.Value = dtpDueDate.Value.Date.AddDays(CType(strCreditDays, Integer))
            Else : cmbVendorID.Text = ""
                'ChkAll.Enabled = False
                fgItems.Cols(2).Editor = Nothing
                Call DisplayData()
            End If
            Call DisplayItems()
            Show_PendingBills(False)
            If _strVoucherType = "DP" Then
                Dim dtbDCNo As DataTable
                If cmDVendor.SelectedIndex >= 0 Then
                    dtbDCNo = DPurchaseInvoice.GetDCNo(cmDVendor.SelectedValue)
                    btLoading = 1
                    Call ClearGrids()
                    btLoading = 0
                    If dtbDCNo.Rows.Count > 0 Then
                        cCmbDcNo.DataSource = dtbDCNo
                        cCmbDcNo.DisplayMember = "DC_No"
                        cCmbDcNo.ValueMember = "DCNo"
                        cCmbDcNo.Splits(0).DisplayColumns(0).Width() = 0
                        cCmbDcNo.SelectedIndex = 0
                    Else
                        cCmbDcNo.ClearFields()
                        cCmbDcNo.Text = ""
                        TextBox1.Text = "0.00"
                        txtSubTotDisply.Text = "0.00"
                        txtSchmDisc.Text = "0.00"
                        txtTax.Text = "0.00"
                        txtGrandTotal.Text = "0.00"
                    End If
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmDVendor_SelectedValueChanged")
            Me.Cursor = Cursors.Default
        Finally
            btLoading = 0
        End Try
    End Sub

    Private Sub dtpDate_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles dtpDate.Validating
        Try
            If dtpDate.Value > Date.Today Then
                BCommon.ShowMsgError(10280)
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "dtpDate_Validating")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub dtpBillDate_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles dtpBillDate.Validating
        Try
            If dtpBillDate.Value > Date.Today Then
                BCommon.ShowMsgError(10281)
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "dtpBillDate_Validating")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub dtpDueDate_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles dtpDueDate.Validating
        Try
            If dtpBillDate.Value > dtpDueDate.Value Then
                BCommon.ShowMsgError(10282)
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "dtpDueDate_Validating")
            Me.Cursor = Cursors.Default
        End Try
    End Sub
#End Region

    Private Sub fgBills_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles fgBills.KeyDown
        If e.KeyCode = Keys.Escape Then
            fgBills.Visible = False
            e.SuppressKeyPress = True
            Call btnClose_Click(Nothing, Nothing)
        End If
    End Sub

    Private Sub fgBills_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles fgBills.KeyUp
        If e.KeyCode = Keys.Escape Then
            fgBills.Visible = False
            e.SuppressKeyPress = True
        End If
    End Sub

    Private Enum ItemGridColumns
        ITEMID = 0
        ITEMCODE = 1
        ITEMNAME = 2

        CLRCODE = 3
        CLRDESC = 4
        PACKID = 5
        PACK = 6
        GODOWNID = 7
        GODOWN = 8

        SCHEMEID = 9
        SCHEME = 10
        QTY = 11

        RATE = 12

        TAXID = 14
        TAX = 15

        AMOUNT = 16
        STATUS = 17
        CLRID = 18
    End Enum

    Private Sub fgItems_ValidateEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.ValidateEditEventArgs) Handles fgItems.ValidateEdit

        If btLoading = 1 Then Exit Sub
        Try
            With fgItems
                Select Case e.Col
                    Case .Cols(ItemGridColumns.RATE).Index
                        If .Editor.Text <> "" AndAlso IsNumeric(.Editor.Text) Then
                            If Convert.ToDouble(.Editor.Text.ToString()) <= 0 Then
                                BCommon.ShowMsgError(10320) 'PLEASE ENTER VALID SALE RATE
                                e.Cancel = True
                                Exit Sub
                            Else

                            End If
                        Else
                            BCommon.ShowMsgError(10239) 'PLEASE ENTER ONLY NUMERIC VALUES.
                            e.Cancel = True
                            .Editor.Text = ""
                            Exit Sub
                        End If
                        'btStatus = 1
                        Call CalculateAmt()
                        'If fgItems.Item(fgItems.RowSel, "STATUS") = "F" Then
                        '    fgItems.Item(fgItems.RowSel, "STATUS") = "U"
                        'End If

                    Case .Cols(ItemGridColumns.QTY).Index
                        '19 .TOTAL STOCK
                        '18 .EMERGENCY STOCK
                        '15 .BATCH QUANTITY
                        If .Editor.Text <> "" AndAlso IsNumeric(.Editor.Text) Then
                            If Convert.ToDouble(.Editor.Text.ToString()) < 1 Then
                                BCommon.ShowMsgError(10232)
                                e.Cancel = True
                                Exit Sub
                            End If
                        Else : BCommon.ShowMsgError(10239) 'PLEASE ENTER ONLY NUMERIC VALUES.
                            e.Cancel = True
                            Exit Sub
                        End If
                        ' btStatus = 1

                        Dim Qty As Integer = IIf(Trim(.Editor.Text) = "", 0, .Editor.Text)

                        Call CalculateAmt()
                    Case .Cols(ItemGridColumns.TAX).Index

                        Call CalculateAmt()
                    Case ItemGridColumns.AMOUNT
                        btLoading = 1
                        fgItems.Col = ItemGridColumns.ITEMCODE
                        fgItems.Row += 1
                        btLoading = 0
                    Case .Cols("SCHEME").Index
                        '1.CHECK FOR VALID OR APPLICABLE SCHEME QUANTITY 
                        '  IF ENTERED SCHEME QUANTITY IS GREATER THANT APPLICABLE QTY THEN ONLY FIRE WARNING MESSAGE 
                        '2.CHECK FOR AVAILABLE SCHEME STOCK
                        'Dim Qty As Integer = .Editor.Text
                        'Dim ApplicableQty As Integer = GetSchemeQty(.Row, .Item(.Row, ItemGridColumns.QTY))
                        'If Qty > ApplicableQty Then
                        '    MessageBox.Show(BCommon.GetMessage(10308).Replace(ItemGridColumns.QTY, ApplicableQty), ConfigurationManager.AppSettings("MsgTitle")) 'Eg. Applicable Schem Quantity is 4 Only.
                        '    .Editor.Text = ApplicableQty
                        'ElseIf Qty > .Item(.Row, "SCHEMESTOCK") Then
                        '    MessageBox.Show(BCommon.GetMessage(10306).Replace(ItemGridColumns.QTY, .Item(.Row, "SCHEMESTOCK")), ConfigurationManager.AppSettings("MsgTitle")) 'ONLY 10 OR ..  SCHEME ITEM QUANTITY IS AVAILBALE.
                        '    .Editor.Text = .Item(.Row, "SCHEMESTOCK")
                        'End If
                        '' btStatus = 1
                        'If fgItems.Item(fgItems.RowSel, "STATUS") = "F" Then fgItems.Item(fgItems.RowSel, "STATUS") = "U"
                End Select
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgItems_ValidateEdit")
        End Try
    End Sub
    Private Sub CalculateAmt()
        Dim intCount As Integer
        Try
            With fgItems
                TotAmount = 0.0
                .Cols(.Col).Item(.Row) = .Editor.Text
                If Not IsNothing(.Cols(ItemGridColumns.QTY).Item(.Row)) OrElse Not IsNothing(.Cols("RATE").Item(.Row)) OrElse Trim(.Cols(ItemGridColumns.QTY).Item(.Row)) <> "" OrElse Trim(.Cols("RATE").Item(.Row)) <> "" Then
                    .Cols("AMOUNT").Item(.Row) = (.Cols(ItemGridColumns.QTY).Item(.Row) * .Cols(ItemGridColumns.RATE).Item(.Row)) + .Cols(ItemGridColumns.TAX).Item(.Row)
                End If
                For intCount = 1 To .Rows.Count - 1
                    If IsNothing(.Cols(ItemGridColumns.ITEMID).Item(intCount)) OrElse Trim(.Cols(ItemGridColumns.ITEMID).Item(intCount)) = "" Then Exit For

                    TotAmount = TotAmount + .Cols("AMOUNT").Item(intCount)
                Next
                txtGrandTotal.Text = Format(TotAmount, "0.00")
                'txtPaidAmt.Text = Format(TotAmount, "0.00")
                'Call CalculateReturnAmt()
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "CalculateAmt")
        End Try
    End Sub
End Class